I have three input texts in my xhtml and I want to check if the sum of those three fields equals to 100. What is the best way of validating multiple fields in my situation? Is it possible to use a validator?
Asked
Active
Viewed 612 times
0
-
2Possible duplicate of [JSF doesn't support cross-field validation, is there a workaround?](http://stackoverflow.com/questions/6282466/jsf-doesnt-support-cross-field-validation-is-there-a-workaround) – BalusC Oct 28 '15 at 13:24
1 Answers
0
...
<h:inputHidden id="inputhiddenId" value="true" validator="#{managedBean.checkSum}"/>
<h:message for="inputhiddenId" style="color: red"/>
...
and
@ManagedBean
@ViewScoped
public class ManagedBean implements Serializable {
...
public void checkSum(FacesContext context, UIComponent component, Object value) {
double val = filed1 + field2 + field3;
if (val != 100.0) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error",
"Sum of fields must be equal to 100");
throw new ValidatorException(msg);
}
}
}
solved my problem.
But also <h:inputText ...
components for field1, field2 and field3 must embrace <f:ajax .../>
.

user2972185
- 231
- 3
- 12