Kiss My App

Monday, May 23, 2005

Validators and Converters in Facelets

Want to wire in a specialized Validator into Facelets with only a few lines of code?

First, lets presume your validator is bound to your JSF Application under the id com.mycompany.RegExpValidator. It has two properties: a String: pattern, and a boolean: caseSensitive.

Now, with your application's Jar, add a single file with a taglib.xml extension:

<facelet-taglib>
<namespace>http://www.mycompany.com/jsf
<tag>
<tag-name>regexp</tag-name>
<validator>
<validator-id>com.mycompany.RegExpValidator</validator-id>
</validator>
</tag>
</facelet-taglib>


Done.

Now you can use it any page and Facelets will take care of wiring all of your properties for you:

<input type="text" jsfc="h:inputText" value="#{login.name}">
<my:regexp pattern="\w+" caseSensitive="false"/>
</input>


Facelets will automatically take care of handling EL also for your UIComponents, Converters, and Validators (any object really), so you could use the built in validateLength, passing in min="#{field.size}".

What to take from this is that there really isn't any need to write tags when Facelets works off of the bean properties of the object you provide it. But if you do want more control, there are base objects and interfaces that you can implement to pretty much do whatever you want in the document.