JSF 2.0 + Hibernate Validação

4 respostas
redthi
bom galera estou tentano fazer validações com o Hibernate e JSF 2.0 segue o código:
testeValidate.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title></title>
    </h:head>
    <h:body>
        <ui:include src="/includes/menu.xhtml"/>
                <h:messages/>
                <h:form>
                  <h:inputText id="jamesName" value="#{james.name}"/>
                  <h:message for="jamesName"/>
                  <h:commandButton value="submit"/>
                </h:form>
                <h:form>
                  <h:inputText id="jamesName" value="#{james.name}" immediate="true"/>
                  <h:message for="jamesName"/>
                  <h:commandButton value="submit"/>
                </h:form>
                <h:form>
                  <h:inputText id="jamesName" value="#{james.name}">
                    <f:validateBean />
                  </h:inputText>
                  <h:message for="jamesName"/>
                  <h:commandButton value="submit"/>
                </h:form>


    </h:body>
</html>
agora meu manegerbean
James.class
@ManagedBean
@SessionScoped
public class James implements Serializable {

  @NotNull
  @Length(min=5, max=10, message="testee")
  private String name;


  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}
queria imprimir a mensage na tela se não atender a obrigatoriedade.

4 Respostas

dev.rafael

Eu ñ tenho certeza absoluta, mas acredito q o JSF 2 se integre com o framework BeansValidation e ñ com o Hibernate Validation, embora um seja derivado do outro eu acredito q eles sejam duas coisas diferentes.

redthi

qual seria minha melhor opção para resolver o problema de validação, alguem pode me dar um caminho ?
alguma dica ou material, sou novo nessa área, agradeceria muito se pudessem só mostrar o caminha de onde estudar…
obrigadoo

benignoms

Não precisa sofrer muito não :smiley:

Faz assim que funciona:

<h:form>  
                   <h:inputText id="jamesName" value="#{james.name}" required="true" requiredMessage="O campo NOME é obrigatório"/>  
                   <h:message for="jamesName"/>  
                   <h:commandButton value="submit"/>  
 </h:form>

Obs.: Quando você indica que o atributo immediate é “true” você esta informando que não quer que passe pela fase de validação.

abraços,

redthi

BOm eu pesquisei e resolvi dessa forma:

<h:form id="form">

                <h:outputLabel value="Nome:"/>
                <h:inputText value="#{clienteMB.cliente.xNome}" id="xNome" maxlength="60" size="60"/>
                <h:message for="xNome" style="background-color: red; "/>

                <h:outputLabel value="Logradouro:"/>
                <h:inputText value="#{clienteMB.cliente.xLgr}" id="XLGR" maxlength="60" size="60"/>
                <h:message for="XLGR"  style="background-color: red;"/>

                <p:commandButton value="Salvar" action="#{clienteMB.salvar}" ajax="false"/>
                <h:commandButton value="Limpar" style=" margin-right: 10px;" type="reset" />
                </h:panelGrid>
                
              </h:form>

e na Class:

@Size(min=4, max=10, message="minimo 5 maximo 10")
    @Column(name = "XNOME")
    private String xNome;
    @Size(min=4, max=10, message="minimo 5 maximo 10")
    @Column(name = "XLGR")

é errado fazer dessa forma ?

Criado 26 de novembro de 2010
Ultima resposta 29 de nov. de 2010
Respostas 4
Participantes 3