How to check if at least one of several input fields is enabled?

I have a form with three fields and a submit button. when the button is pressed, if none are entered in 3 fields, send a verification message. If any of the three fields is entered, process the data and display the results on the same page using the data table. I can send a validation message for one field, but not for two or more fields. Here is my code. Also, if I have a long value that I need to pass, and confirm how I can do this, since the long value cannot be confirmed as isEmpty() or isNull() .

Here is my code, I want to use it with multiple fields and fields with long values ​​that will be checked.

 <h:inputText id="userName" value="#{user.userName}" required="true" requiredMessage="Please enter username" /> <h:inputText id="empId" value="#{user.empId}" required="true" requiredMessage="Please enter Employee Id" /> <h:inputText id="acctNm" value="#{user.acctNm}" required="true" requiredMessage="Please enter Employee Id" /> 
+4
source share
2 answers

Just let the required attribute of each field check for the passed value of the other fields. The presented value is available in the parameter map #{param} client identifier as a key.

Here is an example run:

 <h:form id="form"> <h:inputText id="field1" ... required="#{empty param['form:field2'] and empty param['form:field3']}" /> <h:inputText id="field2" ... required="#{empty param['form:field1'] and empty param['form:field3']}" /> <h:inputText id="field3" ... required="#{empty param['form:field1'] and empty param['form:field2']}" /> </h:form> 

It becomes even more ugly as the number of fields increases.

Alternatively, you can use OmniFaces <o:validateOneOrMore> :

 <h:form id="form"> <h:inputText id="field1" ... /> <h:inputText id="field2" ... /> <h:inputText id="field3" ... /> <o:validateOneOrMore id="oneOrMore" components="field1 field2 field3" /> <h:message for="oneOrMore" /> </h:form> 

Note that performing validation in an action method is a poor design. To do this, you should use standard JSF validation tools such as requiered , validator , <f:validator> and / or <f:validateXxx> .

+6
source

At least iceFaces has another approach to validating multiple fields, especially when validation is more complex than just β€œrequired”, is to use application level validation inside your bean support.

@see Beans Custom Validator

  • Add the validator attribute to you and specify its verification method in your bean support
  • inside bean support you can now access all fields of the form and cross-check

Hello

Rob

0
source

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


All Articles