File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing parameters/ attributes to a custom validator

 
Michael Sullivan
Ranch Hand
Posts: 235
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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>
 
Varun Khanna
Ranch Hand
Posts: 1400
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jignesh Gohel
Ranch Hand
Posts: 276
  • 0
  • Mark post as helpful
  • send pies
  • 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/
 
Vujadin Scepanovic
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • 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");


....

}
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic