0

I'm trying to use Bean validation instead of JSF validation since it is a more DRY aproach, i'm using the annotations on my model but when i insert null data on jsf fields it does nothing...

None of the annotations that i'm using is having any effect, its like they are not there....

I'm using Tomcat 8

My xhtml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<h:body>
<h1>Cadastro de funcionario</h1>
<h:form>
    <h:messages/>
    <h:panelGrid columns="3">
        <h:outputLabel value="Salário: R$ " for="campo-salario"/>
        <h:inputText id="campo-salario" value="#{funcionarioBean.funcionario.salario}">
            <f:convertNumber locale="pt_BR"/>
        </h:inputText>
        <h:message for="campo-salario" />

        <h:outputLabel value="Código: " for="campo-codigo"/>
        <h:inputText id="campo-codigo" value="#{funcionarioBean.funcionario.codigo}"/>
        <h:message for="campo-codigo"/>

        <h:outputLabel value="Data: " for="campo-aniversario"/>
        <h:inputText id="campo-aniversario" value="#{funcionarioBean.funcionario.aniversario}">
            <f:convertDateTime pattern="dd/MM/yyyy" />
        </h:inputText>
        <h:message for="campo-aniversario"/>

        <h:commandButton value="Cadastrar" action="#{funcionarioBean.mensagem}"/>
    </h:panelGrid>

</h:form>
</h:body>
</html>

My Bean

@ManagedBean
public class FuncionarioBean {
    private Funcionario funcionario = new Funcionario();

    public void mensagem(){
        System.out.println("Pressionado");
    }

    public Funcionario getFuncionario() {
        return funcionario;
    }

    public void setFuncionario(Funcionario funcionario) {
        this.funcionario = funcionario;
    }
}

My Funcionario class

import java.util.Date;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

public class Funcionario {
    @NotNull(message="{br.com.k19.Funcionario.nome")
    @Min(value = 0)
    private Double salario;

    @NotNull
    @Min(value = 5)
    @Max(value = 19)
    private Integer codigo;

    @NotNull
    private Date aniversario;

    public Double getSalario() {
        return salario;
    }
    public void setSalario(Double salario) {
        this.salario = salario;
    }
    public Integer getCodigo() {
        return codigo;
    }
    public void setCodigo(Integer codigo) {
        this.codigo = codigo;
    }
    public Date getAniversario() {
        return aniversario;
    }
    public void setAniversario(Date aniversario) {
        this.aniversario = aniversario;
    }

}

My lib Can't post pictures yet. Lib >

antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.6.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-validator-5.0.0.final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
jsf-api-2.2.8.jar
jsf-impl-2.2.8.jar
mysql-connector-java-5.1.23.bin.jar
omnifaces-1.8.1.jar
primefaces-5.0.jar
validation-api-1.0.0.GA.jar

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>K19-Conversao-e-Validacao</display-name>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>

    <context-param>
        <param-name>com.sun.faces.writeStateAtFormEnd</param-name>
        <param-value>false</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>
</web-app>
prabello
  • 556
  • 2
  • 14
  • 31
  • Take a look at http://stackoverflow.com/questions/7545231/notnull-notblank-and-notempty-bean-validation-does-not-work-in-jsf. – Seitaridis Sep 29 '14 at 18:41
  • Updated the question whit more info regarding your link, thanks, but still doesn't work – prabello Sep 29 '14 at 18:54
  • Try to replace some of the `@NotNull` annotations with `@Size(min=1)`. – Seitaridis Sep 29 '14 at 19:06
  • All my fields are numbers, i have #Min and #Max but they don't work either Added #Size(min=1) to salario, nothing happened. – prabello Sep 29 '14 at 19:13
  • I have a working project that uses bean validation. `hibernate-validator-4.0.2.GA.jar` is used there. You have a newer version. – Seitaridis Sep 29 '14 at 19:27

1 Answers1

0

Downloaded hibernate jars again to match the versions

Download validator: http://hibernate.org/validator/

Download ORM: http://hibernate.org/orm/

My lib Lib

Working as intend now

ref: Bean Validation @NotNull, @NotBlank and @NotEmpty does not work in JSF+Tomcat

Community
  • 1
  • 1
prabello
  • 556
  • 2
  • 14
  • 31