Mateus Viccari
Mateus Viccari

Reputation: 7709

JSF Bean validation not working

i have bean validation working if i try to save an object with incorrect values, which means an exception is thrown when i do a Session.save or EntityManager.persist, but the automatic bean validation don't work when i submit the form and there is some fields annotated with annotations like:

@NotEmpty
@NotNull
@Size
etc

I have added all the required .jar files from hibernate validator into my WEB-INF/lib, but it doesn't to work.

Is thee any special thing i should do?

---------EDIT---------- Here is the code of my page (this is the template)

<body>
    <p:growl id="growl" showDetail="true" autoUpdate="true"/>
    <h:form id="formPrincipalTemplate">
        <p:ajaxStatus style="width:40px;height:40px;position:fixed;right:5px;bottom:18px">  
            <f:facet name="start">  
                <p:graphicImage library="bmp" name="gifCarregando.gif" />  
            </f:facet>  

            <f:facet name="complete">  
                <h:outputText value="" />  
            </f:facet>  
        </p:ajaxStatus>

        <ui:insert name="conteudo">
            <!-- Aqui vai o conteúdo da página! -->
        </ui:insert>

    </h:form>
</body>

And here is the page itself (the button that should execute the validation is the one which the word "Gravar" on the value:

<ui:composition template="../template/adminTemplate.xhtml">
    <ui:define name="conteudo">

        <h:panelGroup id="atualiza">
            <div class="btnComandos">
                <p:commandButton value="Novo" actionListener="#{mbAdmin.novo}" disabled="#{!mbAdmin.notModoEdicao}" update=":formPrincipalTemplate:atualiza"/>
                <p:commandButton value="Alterar" actionListener="#{mbAdmin.alterar}" disabled="#{!mbAdmin.notModoEdicao}" update=":formPrincipalTemplate:atualiza"/>
                <p:commandButton value="Gravar" actionListener="#{mbAdmin.gravar}" disabled="#{mbAdmin.notModoEdicao}" update=":formPrincipalTemplate:atualiza"/>
                <p:commandButton value="Excluir" actionListener="#{mbAdmin.excluir}" disabled="#{!mbAdmin.notModoEdicao}" update=":formPrincipalTemplate:atualiza" onclick="return confirm('Deseja realmente excluir?');"/>
                <p:commandButton value="Cancelar" actionListener="#{mbAdmin.cancelar}" disabled="#{mbAdmin.notModoEdicao}" update=":formPrincipalTemplate:atualiza"/>
            </div>
            <br/>
            <p:tabView id="tabView" orientation="left" style="width:80%;margin:auto;" activeIndex="#{mbAdmin.aba}">

                <p:tab title="Consulta">
                    <p:dataTable id="admins" var="adm" value="#{mbAdmin.admins}" rowKey="#{adm.adminCodigo}"  
                            selection="#{mbAdmin.admin}" selectionMode="single">

                        <p:ajax update=":formPrincipalTemplate:atualiza" event="rowSelect" listener="#{mbAdmin.mudarAba}"/>

                        <p:column width="80">
                            <f:facet name="header">
                                <h:outputText value="Código" />
                            </f:facet>
                            <h:outputText value="#{adm.adminCodigo}" />
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="Nome" />
                            </f:facet>
                            <h:outputText value="#{adm.adminNome}" />
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="Email" />
                            </f:facet>
                            <h:outputText value="#{adm.adminEmail}" />
                        </p:column>

                    </p:dataTable>
                </p:tab>

                <p:tab title="Cadastro" id="formulario">
                    <h:panelGrid columns="2">
                        <h:outputText value="Código:"/>
                        <h:outputText value="Nome:"/>

                        <p:inputText value="#{mbAdmin.admin.adminCodigo}" disabled="true"/>
                        <p:inputText value="#{mbAdmin.admin.adminNome}" disabled="#{mbAdmin.notModoEdicao}"/>
                    </h:panelGrid>
                    <h:panelGrid columns="2">
                        <h:outputText value="Email:"/>
                        <h:outputText value="Senha:"/>

                        <p:inputText value="#{mbAdmin.admin.adminEmail}" disabled="#{mbAdmin.notModoEdicao}"/>
                        <p:inputText value="#{mbAdmin.admin.adminSenha}" disabled="#{mbAdmin.notModoEdicao}"/>
                    </h:panelGrid>
                </p:tab>

            </p:tabView>
        </h:panelGroup>
    </ui:define>
</ui:composition>

Upvotes: 0

Views: 433

Answers (1)

Andy_Lima
Andy_Lima

Reputation: 159

I would like to ask this as a comment but my reputation is to low, for that reason i ask here.

Can you write things to your database over your application? Are the columns of your database same annotated like your entity file?

I never saw a .jar file in WEB-INF/lib therfore I use the "Dependencies" folder. To add sth. to this folder you rightklick the folder Dependencies -> Add Dependency, better you directly use a maven project there you can put all your dependencies in a pom file and maven will do the rest for you. The dependency for maven you'll find easy over Goolge, just search for "hibernate dependency". If you use a maven project you never take care about .jar files again. Just add the dependency for whatever you like to use and maven will download everything and put it in the right place that it is available in your project. The only thing you need to do is to fix your imports, after you wrote the code, that the packages are avaiable for your class. I hope this was more or less understandable?

Upvotes: 1

Related Questions