kothvandir
kothvandir

Reputation: 2161

@Email hibernate validator annotation not working in JSF 2 Bean

I have a JSF 2 application and I cannot make work a @Email validation on a JSF ManagedBean attribute.

This is my stack:

I've checked in my web-inf/class I have these libraries as recomended here:

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:

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.

Upvotes: 1

Views: 1137

Answers (1)

BalusC
BalusC

Reputation: 1108742

GlassFish as being a Java EE container already ships with among others JSF and Bean Validation out the box. Upgrading them should be done by replacing the JARs in /glassfish/modules folder.

If this is not possible for some reason, and you really need to upgrade them via the webapp, then you need to instruct GlassFish to look in the webapp first for the libraries by adding the following entry to glassfish-web.xml:

<class-loader delegate="false" />

Otherwise GlassFish will still load them from own libs and/or classloading trouble will occur because there are duplicate classes with the same name, resulting in confusion and conflicts between libraries in an application with multiple classloaders, such as instanceof falsely returning false on an instance of a class loaded by another classloader having access to the duplicate library.

Upvotes: 1

Related Questions