开发者

Conditional Mandatory fields in Richfaces

开发者 https://www.devze.com 2023-04-11 11:49 出处:网络
I am using Richfaces 3.3.3 for my application. I am not sure how to achieve conditional mandatory check in rich faces. Below is my scenario:

I am using Richfaces 3.3.3 for my application. I am not sure how to achieve conditional mandatory check in rich faces. Below is my scenario:

I have 3 text boxes input1,input2,input3 as follows:

<h:inputText id="input1" value= "#{mybean.input1}" required= "true" maxlength="350" >
  <mj:regexValidator pattern="[0-9A-Za-z% ]*" />
</h:inputText>

<h:inputText id="input2" "#{mybean.input2}"maxlength="350" ></h:inputText>

<h:inputText id="input3" "#{mybean.input3}"maxlength="350"></h:inputText>
<a4j:commandButton value="save" action="#{mybean.saveData}"/>

In the above code, "input1" is a required field accepting only alpha numeric value. "input2" is optional field and "input3" is required only if user enters "input2" otherwise "input3" is also optional

Currently, I am checking input2 and input3 on saveData method on backing bean (on click of save button) and throw a error message to scree开发者_C百科n.

But the following scenario fails, If user enters a special charecter like "####" in input1, and enters a valid value in input2 but leaves input3 as empty, I receive only error message related to input1 on screen.

This is because the regex validation is triggered even before reaching save method in backing bean.

Is there any way to overcome this method. Please advice


You could fix this by doing the validation in the backend saveData() method and create messages from there. JSF will handle them the same way as other validations but then you have more control.

FacesContext.getCurrentInstance().addMessage("info", new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));

or

context.addMessage(fullFieldId, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));

MAG, Milo van der Zee

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号