Get blank lines from <h: inputText> pass check

Is there a way to get jsf validator to process empty lines? I have a special validator for all inputTexts. Instead of implementing the โ€œrequiredโ€ tag, I would like the client handler to process empty lines and determine within the bean if this field is required 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 each input text in my application:

 <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 necessity or not, therefore, the problem of component dependency does not matter. My goal was to pass the null value through the validator, so I don't need to add both tags to the inputText.

In addition, the Validator handler receives the โ€œcomponentโ€ as a parameter, which means that I can get its identifier and see it in real time if necessary. (The same component may not always be required).

If I use required="#{bean.requireCheck}" (for an example), can I somehow get the component from which it was called?

+4
source share
1 answer

JSF 1.x by default does not work with filtering empty fields. The required attribute must be used for this. If your specific problem is that the second field should be set to required whenever the first field is populated, then you should just check it like this in the required attribute.

 <h:inputText value="#{bean.input1}" binding="#{input1}" /> <h:inputText value="#{bean.input2}" required="#{not empty input1.value}" /> 

If this is not a functional requirement, then you should clarify what is more in the question so that a more suitable answer can be given.


Refresh . So you want to control model requirements? Add Map<String, Boolean> to the bean as follows:

 private Map<String, Boolean> required; public Bean() { required = new HashMap<String, Boolean>(); required.put("foo", true); required.put("bar", false); // false entries are by the way not necessary. // ... } public Map<String, Boolean> getRequired() { return required; } 

Use it as follows:

 <h:inputText id="foo" required="#{bean.required['foo']}" /> <h:inputText id="bar" required="#{bean.required['bar']}" /> 

If you are using JSF 2.0, you can do it as follows

 <h:inputText id="foo" required="#{bean.required[component.id]}" /> <h:inputText id="bar" required="#{bean.required[component.id]}" /> 

Unrelated to a specific problem, it is illegal to set a <h:message> as a child of <h:inputText> . Place them next to each other.

+6
source

Source: https://habr.com/ru/post/1343170/


All Articles