Is there a way to make jsf validator handle empty strings?
I have a custom validator for all inputTexts.
Instead of having to implement the "required" tag as well I would like to have the customer handler handle the empty strings and determine within the backing bean if that field is mandatory or not.
<h:inputText id="SSID_STR" styleClass="propertyInput" value="#{wifiDM.SSIDStr}" validator="#{wifiDM.validate}" >
<h:message for="SSID_STR" fatalClass="mandatoryFieldMissing" tooltip="true" />
</h:inputText>
If I had to add the required tag I would have to do this for every input text in my app:
<h:inputText id="SSID_STR" styleClass="propertyInput" value="#{wifiDM.SSIDStr}" validator="#{wifiDM.validate}" required="#{wifiDM.isRequired} requiredMessage="*">
<h:message for="SSID_STR" fatalClass="mandatoryFieldMissing" tooltip="true" />
<f:attribute name="InputID" value="SSID_STR">
</h:inputText>
which is a mess…
Update (Clarification)
First of all, using JSF 1.2.
Each field is independent in the sense of being required or not so the issue of component dependency is irrelevant.
My goal was to pass the null value through the validator so I don't have to add both tags to the inputText.
Also, The Validator handler gets 'component' as a parameter, which means I can get its ID and see in real time if it is required. (The same component may not always be required).
If I use required="#{bean.requireCheck}"
(For Example) can I somehow get the component from which it was called?
Best Answer
JSF 1.x does by default not fire validators on empty fields. For that the
required
attribute has to be used. If your concrete problem is that the second field has to be setrequired
whenever the first field is been filled in, then you should just check that so in therequired
attribute.If that's not the functional requirement, then you should clarify that more in the question so that a better suited answer can be given.
Update: So you want to control the requireness from the model on? Add a
Map<String, Boolean>
to the bean as follows:Use it as follows:
If you were using JSF 2.0, you could do it as follows
Unrelated to the concrete problem, it's illegal to put a
<h:message>
as child of<h:inputText>
. Put them next to each other.