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?