Reputation: 31
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 "[email protected]", this works ok. Then I add "[email protected]", this also works ok. Then I change "[email protected]" to "", this throws a validation exception, which is shown on the screen, and everything is still ok. But then I correct the "" with "[email protected]" and submit, this time all inputText start showing "[email protected]", even when I add a new InputText, which also shows "[email protected]".
It seems that when the validation fail, all components inside ui:repeat get bound to the value of the last item. Any thoughts?
Upvotes: 2
Views: 1264
Reputation: 31
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>
Upvotes: 1