Forums Register Login

Passing parameters/ attributes to a custom validator

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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>
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Just found out this link on how to parametrize JSF custom validators

http://blog.projectnibble.org/2008/07/26/parameterized-jsf-facelets-validators/
+Pie Number of slices to send: Send
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");


....

}
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 27430 times.
Similar Threads
JSP 1.1 vs JSP 1.2
interview question
how to pass Parameter in custom tag
Using ValueExpression's in a validator tag
On the brink of madness
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:14:10.