2

I have the following xhtml, validator, and managedBean:

<h:form id="form">
<ui:repeat var="item" value="#{myBean.usersEmail}" varStatus="status">
    <p:inputText id="userEmail" value="#{item.email}">
        <f:validator validatorId="MyValidator"/>
    </p:inputText>

    <p:commandButton value="++++" update=":form" action="#{myBean.addEmail()}"  />

</ui:repeat>

</h:form>

@FacesValidator("MyValidator")
public class ValidationClass extends Validator {

    @Override
    public void validate(FacesContext ctx, UIComponent component, Object value) throws ValidatorException {
    String email = value.toString();
    EmailValidator validator = EmailValidator.getInstance();



    if(StringUtils.isNotBlank(email) && !validator.isValid(email)) {


        FacesMessage message = new FacesMessage();
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        message.setSummary("Email is not valid.");
        message.setDetail("Email is not valid.");
        ctx.addMessage("userEmail", message);

        throw new ValidatorException(message);
    }
}
}

@ManagedBean
public class MyBean{

    @Getter
    @Setter
    List<UserEmail> usersEmail = new ArrayList<UserEmail>();

    public void addEmail(){
        usersEmail.add(new UserEmail());
    }
}

public class UserEmail{

    @Getter
    @Setter
    String email = "";

}

The email addition works fines until the first validation fail. When this happens, all inputText components show the same values. For example, first I add "user1@gmail.com", this works ok. Then I add "user2@gmail.com", this also works ok. Then I change "user1@gmail.com" to "", this throws a validation exception, which is shown on the screen, and everything is still ok. But then I correct the "" with "user3@gmail.com" and submit, this time all inputText start showing "user2@gmail.com", even when I add a new InputText, which also shows "user2@gmail.com".

It seems that when the validation fail, all components inside ui:repeat get bound to the value of the last item. Any thoughts?

  • You use the same id="userEmail" for all input, Did you try without id? – user3489875 Jul 08 '14 at 07:57
  • I did indeed, but the problem still persists. Although I am using the same id in the xhtml, in the html genarated by JSF, the id is concatenated with the ui:repeat item id. For example, "0:userEmail", "1:userEmail", "2:userEmail", etc. So I don't think the problem is that. – Joao Henrique Jul 08 '14 at 12:22
  • We are facing exactly **the same** Issue. Exact the same scenario and usecase. We downgraded to 2.2.5 again and the Issue is gone. So it has to be a bug in Mojarra 2.2.6 – dognose Jul 17 '14 at 15:45
  • Upgraded to 2.2.7 - There the issue is fixed. So it really just affects 2.2.6. I could not find any related fix in the changelogs of 2.2.7, so it's maybe just a sideeffect of another bug that was present in 2.2.6 – dognose Jul 17 '14 at 16:34
  • It may have been related to this Issue: https://java.net/jira/browse/JAVASERVERFACES-3274 – dognose Jul 17 '14 at 16:39

1 Answers1

1

I changed my implementation to use the c:forEach tag from JSTL and now it's working fine, even on Mojarra 2.2.6, here it's what I did:

 <c:forEach var="item" items="#{myBean.usersEmail}" varStatus="status">


 <p:inputText id="id${status.index}"   value="${item.email}" validator="MyValidator" />

 <p:message for="id${status.index}" />

 <p:commandButton value="+" update=":form" action="#{myBean.addEmail()}"  />

 </c:forEach>