It's not a secret anymore!*
The moose likes JSF and the fly likes Passing parameters/ attributes to a custom validator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Passing parameters/ attributes to a custom validator" Watch "Passing parameters/ attributes to a custom validator" New topic
Author

Passing parameters/ attributes to a custom validator

Michael Sullivan
Ranch Hand

Joined: Dec 26, 2003
Posts: 235
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

Joined: Dec 26, 2003
Posts: 235
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

Joined: May 30, 2002
Posts: 1400
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.


- Varun
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
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 ]

SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
Just found out this link on how to parametrize JSF custom validators

http://blog.projectnibble.org/2008/07/26/parameterized-jsf-facelets-validators/

Regards,
Jignesh

The Art Of Life Is To Know When To Be Useless And When To Be Useful - CHUANG TZU
Vujadin Scepanovic
Greenhorn

Joined: Aug 27, 2010
Posts: 1
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");


....

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing parameters/ attributes to a custom validator
 
Similar Threads
Using ValueExpression's in a validator tag
how to pass Parameter in custom tag
JSP 1.1 vs JSP 1.2
On the brink of madness
interview question