• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Passing parameters/ attributes to a custom validator

 
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The goal: To validate that two email addresses match each other (or passwords, or whatever). What I've been toying with is creating a custom validator that takes a parameter, and compares it to the JSF object.toString() that it was bound to... thus comparing two strings.

A coworker suggested that I map the objects using the facesContext to the backing bean for the JSP, and then access the values in the validator. However, I thought it cleaner if I just wrote a custom validator that compared two strings... without binding and such.


My questions are:
1. can you write a custom validator that takes a parameter/ attribute?
2. I've got one coded up, but the JSP that implements it says the body must be empty for f:validator

Thanks in advance for any advice, suggestions, and pointers!
[ March 29, 2005: Message edited by: Michael Sullivan ]
 
Michael Sullivan
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a few more details:

javax.servlet.ServletException: /LocationEditPage.jsp(109,2) Body is supposed to be empty for f:validator

...

<h:inputText id="email_verification" value="#{pc_LocationView.email_address}">
<f:validator validatorId="email" />>
<f:validator validatorId="compareTwoStrings">
<f:attribute name="compareTo" value="myEmail@yahoo.com" />
</f:validator>
</h:inputText>

...

<validator>
<validator-id>compareTwoStrings</validator-id>
<validator-class>com.adt.ecv.validators.CompareTwoStrings</validator-class>
<attribute>
<attribute-name>compareTo</attribute-name>
<attribute-class>java.lang.String</attribute-class>
</attribute>
</validator>
 
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can customize the validator to have two attribute, something like
<v:myValidator firstArg="ABCD" secondArg="WXYZ" />

and you can get these two values in the validate() method of validator and you can do whatever you want.
 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how is the paramater retreived in the validate method ? is there any direct method ? i found few articles on creating custom validators, but couldnt find any which take attributes


thanks,
Rajeev
[ May 21, 2006: Message edited by: Rajeev Ravindran ]
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just found out this link on how to parametrize JSF custom validators

http://blog.projectnibble.org/2008/07/26/parameterized-jsf-facelets-validators/
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should place "attribute" tag inside the component, NOT validator.

Do it like this:

<h:inputText id="email_verification" value="#{pc_LocationView.email_address}">
<f:validator validatorId="email" />
<f:validator validatorId="compareTwoStrings"/>

<f:attribute name="compareTo" value="myEmail...yahoo.com" />

</h:inputText>

...
<validator>
<validator-id>compareTwoStrings</validator-id>
<validator-class>com.adt.ecv.validators.CompareTwoStrings</validator-class>
<attribute>
<attribute-name>compareTo</attribute-name>
<attribute-class>java.lang.String</attribute-class>
</attribute>
</validator>


Then, inside "validate" method of class "CompareTwoStrings", do this:

public void validate(FacesContext facesContext, UIComponent component, Object value)
throws ValidatorException {

.....

String email = (String) component.getAttributes().get("compareTo");


....

}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic