Mojarra 2.1
I need to stop validating an input value after one of the validators failed. Here is what I tried:
<h:inputText id="filterName" value="#{bean.name}" >
<f:validator validatorId="simpleNameValidator" />
<f:validator binding="#{customValidator}" disabled="#{facesContext.validationFailed()}"/>
</h:inputText>
The validators:
public class SimpleNameValidator implements Validator{
private static final int MAX_LENGHT = 32;
@Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
String v = (String) value;
FacesMessage msg;
if(v.equals(StringUtils.EMPTY)){
msg = new FacesMessage("Name is empty");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
if(v.length() >= MAX_LENGHT){
msg = new FacesMessage("Name is too long"));
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
}
and
public class CustomValidator implements Validator{
private NameService nameService;
@Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
String name = nameService.getName((String) value);
if(name != null) {
FacesMessage msg = new FacesMessage("The name already exists");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
public NameService getNameService() {
return nameService;
}
public void setNameService(NameService nameService) {
this.nameService = nameService;
}
}
But it didn't work. I didn't understand why it didn't, because I explcitly specified the disabled
attribute of the second validator. So, it must not have been even applied, because the first one failed. Maybe I misunderstood the validationFailed's purpose...
Couldn't you explain that behavior and how to fix that?