Reputation: 2921
my english is not good,sorry about that.My question is about jsf validation.When my validation bean throws ValidatorException,my managed bean save_method works.but i dont want that my save_method works.if email is not valid , my save_method shouldnt work.
composite interface
<composite:attribute name="validator" required="false"
method-signature="void Action(javax.faces.context.FacesContext, javax.faces.component.UIComponent,Object)" />
composite implementation like that
<c:if test="#{cc.getValueExpression('validator')!=null}">
<p:inputText value="#{cc.attrs.value}" id="#{cc.attrs.id}"
required="#{cc.attrs.required}" validator="#{cc.attrs.validator}"/>
validate method in validation bean.
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String email = (String) value;
Pattern mask = null;
mask = Pattern.compile(EMAIL_REGEX);
Matcher matcher = mask.matcher(email);
if (!matcher.matches()) {
messageManager.warn(MessageManager.Bundles.WO, "validator.message.email");
throw new ValidatorException(new FacesMessage("hoppala"));
}
}
and my managed bean save method.
@Override
public boolean save() {
super.save();
}
Upvotes: 2
Views: 2544
Reputation: 3509
Are you working with mojarra below version 2.1.10? There was a bug that got fixed recently. So if you can, update to latest which is 2.1.13. But anyhow, in JSF you wouldn't do it the way you described it. I would recommend to apply the validator by using <f:validator/>
, like this:
<mytags:customInput value="#{validationModel.value}">
<f:validator for="input" validatorId="org.validationexample.EmailValidator" />
</mytags:customInput>
And your composite component would have to implement <cc:editableValueHolder>
:
<cc:interface>
<cc:attribute name="value"/>
<cc:editableValueHolder name="input" targets="inputText"/>
</cc:interface>
<cc:implementation>
<p:inputText id="inputText" value="#{cc.attrs.value}" />
</cc:implementation>
And finally your validation is handled by a dedicated validator class:
@FacesValidator("org.validationexample.EmailValidator")
public class EmailValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
// your validation here
}
}
Anyhow, there are other approaches/flavours as well. You can place <f:validator>
also directly into your composite component inside <p:inputText>
. Since <f:validator>
has a disabled
attribute you can disable it on demand. Another approach would be to use Bean Validation and take the existing EmailValidator that comes with Hibernate Validator. In that case you just need to set the annotation @Email above your property.
Upvotes: 3