Not quite what I expected…JSF & Validation
So I’m working on a simple search screen (for querying LDAP attributes). There is a Googley type interface (simple box with a search button beside it). The LDAP throws a fit if you search on ‘null’ (the value assigned to a text box in JSF when no value has been entered). JSF field tags support a ‘required’ attribute which will validate them. The validation message is, well, brutal.
‘Validation Error: Input Required’
No big deal…I’ve coded lots of custom JSF validators before (for other types of validation), I’ll put a validation method on the pagecode, reference it in the field and be done with it. Here’s the method I created:
public void validateQueryString(FacesContext context, UIComponent toValidate, Object value) {
String queryString = (String) value;
if (queryString == null || queryString.trim().length()<1){
((UIInput) toValidate).setValid(false);
FacesMessage message = new FacesMessage("I'm sorry, I can't make something from nothing...please enter a search string");
context.addMessage(toValidate.getClientId(context), message);
}
}
If you’re not familiar with JSF, let me walk you through…it casts the object being validated into a String and then checks to see if it’s null or empty…if it is, it sets ths validator to invalid, binds a message to the field and drops out of the Validation phase of the lifecycle.
I brought up the form, hit ‘Search’ without entering a value in the field and expected to see my cheeky validation message…I see a roar of red text ripping along the console…the Validation passed, the LDAP is getting queried?!?! That wasn’t supposed to happen
I checked the method name and confirmed it was the same as the reference on the jsp…yep, matched.
I tried again, only this time, I entered a single space in the text field (knowing I’d trim the string and therefore fail validation)…the method was called, my cheeky message appeared…hmmm. Validation failed as expected
I was coming to the conclusion that JSF doesn’t event create the field for validation if it’s null when I stumbled upon this tread.
Are you kidding me?!
The part I really don’t understand is the response from an architect:
“Hi,
This is what our architect had to say regarding the validator not getting invoked when no value has been entered in a field:
“JSF validators are *not* invoked on empty fields. The general reasoning is that, if there is no input data, then there is nothing to validate. Therefore, the components are working as they should for this use case.”
Hope this helps
Thanks,
Creator Team.”
I’m scratching my head on this one…not because it’s a road block (I could just live with the Eastern block type validation message), but because it throws off what I believed was a pretty solid validation framework.
JSF is self centered when it comes to validation (it wants to do ALL the heavy lifting) which is something I’ve learned to live with. It makes validation testing a pain but again, you learn to live with the cards you’re dealt. I figured I could live with JSF Validation provided I had full control over how it’s used and now I don’t believe that to be the case.
If anyone out there can shed some light on my interpretation of JSF validation on empty fields, I’d really appreciate your input.
Originally published at http://jamiemcilroy.wordpress.com on October 10, 2006.