advertisement

JSF: MultipleField Validations

At work we decided that we wanted all our validations on a page to happen within the validation phase of JSF.

On trying this we got a lot of trouble from fields that had such a relation to other fields that you could only validate them as a whole.

The problem here is that in JSF a validator is designed to validate just one component, so in general just one field. Unless you choose to write components for each serie of fields that are relative to each other. So for validating multiple fields the general trick would be to declare a hidden field component with a validator underneath. This would get the components by id and validate it’s value. The problem with this method is that you can’t re-use this validator. I’will try to explain the problem with an example: an adress validator.

Imagine a page with the following fields:


<h:inputText id="street" value="#{backingBean.street}"/>
<h:inputText id="housenumber" value="#{backingBean.houseNumber}"/>
<h:inputText id="zipCode" value="#{backingBean.zipCode}"/>
<h:inputText id="city" value="#{backingBean.city}"/>

Probably you would want to check if the combination of zipcode, street, housenumber and city are valid. To do this you could introduce a hidden field with a validator underneath:

<h:inputHidden  >
    <ns:adressValidator>
</h:inputHidden>

in this validator you should have the code to get the desired components by id. So you would use the street’s id the housenumber’s id, etc. The obvious problem here is that you would create a dependency of the id’s that you use. You could say that when you put an adress on a page you will always use certain id’s, like street etc. So u can re-use the adres validator on multiple pages, but (apart from the inevitable typo’s), you will also get into trouble when there is more than one adres on a page (for example delivery and billing adress). In this case you would have to write a second validator that would use the second set of id’s etc etc.

Since I didn’t choose writing validators for a hobby, I had to come up with something else: a re-usable multiple field validation. And while we were at it, instead of making it a validator we made it a component.

A component on which there were two properties: keys and forComponents.

The keys are the values that you would expected in the validator, so mostly functional id’s like street, housenumber etc.

The forComponents were the id’s of the component you’ll want to bind to the keys.

In the component both fields would be parsed and the local-values of the components would be stored under the key in a map. This way a validator could simply ask the key-value pairs from the component and validate the fields in relation to each other.

Now you can do the following:


<h:inputText id="street" value="#{backingBean.street}"/>
<h:inputText id="housenumber" value="#{backingBean.houseNumber}"/>
<h:inputText id="zipCode" value="#{backingBean.zipCode}"/>
<h:inputText id="city" value="#{backingBean.city}"/>
<mycom:multipleFieldComponent keys="street,houseNumber,zipCode,city"  forComponents="street,housenumber,zipcode,city">
 <mycom:adresValidator/>
</mycom:MultipleFieldComponent>

and on the same page:

<h:inputText id="street2" value="#{backingBean.street2} />
 <h:inputText id="housenumber2" value="#{backingBean.houseNumber2} />
 <h:inputText id="zipCode2" value="#{backingBean.zipCode2} />
 <h:inputText id="city2" value="#{backingBean.city2} />
<mycom:multipleFieldComponent keys="street,houseNumber,zipCode,city" forComponents="street2,housenumber2,zipcode2,city2">
    <mycom:adresValidator/>
</mycomMultipleFieldComponent>

You can re-use the adresValidator on different pages and mutiple times on the same page.

This really works well if you want to validate in the validation phase of JSF, like I sometimes did. The (easier) alternative offcourse is validating in the process phase.

To make your validations even more flexible you can choose to add one more attibutes to the multipleFieldComponent: validatorName. This for example can be used to give the component the name of the SpringBean that will be used as validator. Now you would never have to implement a JSF Validator for multiple field validation again and you can re-use the validation logic in the business-layer without any problem.

This is the solution we chose when we didn’t have an option but to validate in the validation phase. The ongoing discussion of where to validate and where not would be interesting enough for another entry I think ;-)

If you solved validation issues within JSF or any other component based webframework in other ways, I would love to hear from you.

Cheers.

3 comments to JSF: MultipleField Validations

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>