I have a JSF 2 application and I cannot make work a @Email validation on a JSF ManagedBean attribute.
This is my stack:
- Glassfish 3.1.1
- Mojarra 2.2.9 / JSF 2.2
I've checked in my web-inf/class I have these libraries as recomended here:
- javax.validation:validation-api:jar:1.0.0.GA
- org.hibernate:hibernate-validator:jar:4.3.2.Final
My code:
<h:body>
<!-- <h:log id="log" /> -->
<h:messages globalOnly="true" style="color:red;margin:8px;" />
<h:form id="form">
<f:validateBean>
<h:outputLabel for="email" value="Enter your email: " />
<h:inputText id="email" value="#{userEditBean.email}" />
<h:message for="email" />
<h:commandButton type="submit" value="Enviar"
action="#{userEditBean.submit()}" />
</f:validateBean>
</h:form>
The managed bean:
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.validation.constraints.Pattern;
@ManagedBean
@SessionScoped
public class UserEditBean {
private static final String CURRENT_PAGE = "";
private static final String USER_LIST_PAGE = "userList";
//@Pattern(regexp = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+", message = "Email format is invalid.")
@Email(message = "Email format is invalid.")
private String email;
public String submit() {
return CURRENT_PAGE;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Notes:
- JSR 303 standard validations (@Null, @NotNull, ...) work perfectly, I can do email validation with a @Pattern as is showed here, but I want to know why other hibernate validator annotations don't work.
- Hibernate validator works perfectly if I try to insert a model with an invalid email in a property annotated with @Email, hibernate throws an exception in preinsert validation.
- I've tried annotating field or getter as is mentioned here, neither of them worked.
- I've gone over the hibernate validator reference integration chapter, but it just says you have to use the tag to force validation.
Is it possible to use additional annotations from hibernate validator in a JSF bean or just the javax.validation.constraints.* standard ones? (without using validator programmatically)
UPDATED: 2015-03-23: in response to BalusC answer:
Glassfish 3.1.1 comes with hibernate-validator bundle and it's the same version I'm using, I can see this in server startup log:
2015-03-23T14:11:24.772+0100|Información: Inicializando Mojarra 2.2.9 (-SNAPSHOT 20141218-0939 https://svn.java.net/svn/mojarra~svn/tags/2.2.9@14083) para el contexto '/fwk4jsf'
2015-03-23T14:11:25.368+0100|Información: HV000001: Hibernate Validator 4.3.2.Final
And I also have the delegate="true" in my glassfish-web.xml, but it doesn't work either.
Thank you.