-
Available since OmniFaces 1.7
The <o:validateMultiple>
allows the developer to validate multiple fields by either a custom validator method:
<o:validateMultiple id="myId" components="foo bar baz" validator="#{bean.someMethod}" />
<h:message for="myId" />
<h:inputText id="foo" />
<h:inputText id="bar" />
<h:inputText id="baz" />
whereby the method has the following signature (method name is free to your choice):
public boolean someMethod(FacesContext context, List<UIInput> components, List<Object> values) {
// ...
}
Or, by a managed bean instance which implements the MultiFieldValidator
interface:
<o:validateMultiple id="myId" components="foo bar baz" validator="#{validateValuesBean}" />
<h:message for="myId" />
<h:inputText id="foo" />
<h:inputText id="bar" />
<h:inputText id="baz" />
@ManagedBean
@RequestScoped
public class ValidateValuesBean implements MultiFieldValidator {
@Override
public boolean validateValues(FacesContext context, List<UIInput> components, List<Object> values) {
// ...
}
}
Design notice
Note that this validator does not throw ValidatorException
, but returns a boolean! Message handling and invalidation job is up to the ValidateMultipleFields
implementation who will call this method. You can customize the message by the message
attribute of the tag. Refer ValidateMultipleFields
documentation for general usage instructions.
Demo
Demo source code
<h:form>
<h3>Please enter "one", "two" and "three" respectively.</h3>
<o:validateMultiple id="validateMultiple" components="foo bar baz" validator="#{validateMultipleImpl}"
message="You should enter 'one', 'two' and 'three' respectively." />
<h:panelGrid columns="3">
<o:outputLabel for="foo" value="Foo" />
<h:inputText id="foo" />
<h:message for="foo" />
<o:outputLabel for="bar" value="Bar" />
<h:inputText id="bar" />
<h:message for="bar" />
<o:outputLabel for="baz" value="Baz" />
<h:inputText id="baz" />
<h:message for="baz" />
<h:panelGroup />
<h:commandButton value="submit">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
<h:panelGroup>
<h:message for="validateMultiple" />
<h:outputText value="OK!" rendered="#{facesContext.postback and not facesContext.validationFailed}" />
</h:panelGroup>
</h:panelGrid>
</h:form>
package org.omnifaces.showcase.validators;
import java.util.List;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.faces.component.UIInput;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
import org.omnifaces.validator.MultiFieldValidator;
@Named
@ApplicationScoped
public class ValidateMultipleImpl implements MultiFieldValidator {
@Override
public boolean validateValues(FacesContext context, List<UIInput> components, List<Object> values) {
return "one".equals(values.get(0)) && "two".equals(values.get(1)) && "three".equals(values.get(2));
}
}
Documentation & Sources