1

I have problem to validate more than one component in h:form ... I need to validate these 2 inputText components but if I do it like this:

    <p:inputText id="actionNameInput"
                 title="TO DO"
                 value="#{repositoryBean.newActionName}"
                 label="Action name"
                 required="true"
                 requiredMessage="Action name is missing.">
        <f:validator validatorId="inputTextValidator"/>
        <f:attribute name="input1" value="Action name" />
    </p:inputText>

    <p:inputText id="identifierInput"
                 title="TO DO"
                 value="#{repositoryBean.newActionRegex}"
                 label="Identifier"
                 required="true"
                 requiredMessage="Identifier is missing.">
        <f:validator validatorId="inputTextValidator"/>
        <f:attribute name="input1" value="Identifier" />
    </p:inputText>

here is validator class:

@FacesValidator(value = "inputTextValidator")

public class AddActionValidatorInputText implements Validator{
    @Override
    public void validate(FacesContext facesContext, UIComponent uiComponent, Object o) throws ValidatorException {

        if(((String)o).length() < 3){
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    uiComponent.getAttributes().get("input1")+" should be longer than 3characters.", null);
            FacesContext.getCurrentInstance().addMessage(null, message);
            throw new ValidatorException(message);
        }
    }
}

it will only validate first inputText ... I have tryied this, but I cant get values from components(always null) ... I read smthing about seam-faces, but If I applied it, there was some bigger error(like it needs more and more dependecies...) . I do not want to validate it in my bean class.

S. Emjuel
  • 11
  • 2

1 Answers1

0

Here you can find possible solutions of the problem. And to make values not null, use valueChangeListener attribute like this:

 <p:inputText id="actionNameInput"
             title="TO DO"
             value="#{repositoryBean.newActionName}"
             label="Action name"
             required="true"
             requiredMessage="Action name is missing."
             valueChangeListener="#{repositoryBean.onNewActionNameChange}">
    <f:validator validatorId="inputTextValidator"/>
    <f:attribute name="input1" value="Action name" />
</p:inputText>

and in backing bean:

public void onNewActionNameChange(ValueChangeEvent event) {
    setNewActionName(event.getNewValue().toString());
}

This article about JSF lifecycle may help you to understand what is going on inside.

T. Kryazh
  • 25
  • 1
  • 10