problem with Java validation

Asked

Viewed 58 times

1

I’m implementing validationBean in my project, and I’m having trouble validating a field, but apparently it’s all right in the settings I made in my project as you can see:

Bundle:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.0.1.Final</version>
    <scope>compile</scope>
</dependency>

Look at the field in my entity:

@NotNull
@Column(precision = 10, scale = 2, nullable = false)
private BigDecimal valorImovel;

And look how it’s on my page:

<ui:composition template="/WEB-INF/template/LayoutPadrao.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

    <ui:define name="titulo">Novo produto</ui:define>

    <ui:define name="corpo">
        <h:form>

            <h1>Novo produto</h1>

            <p:messages autoUpdate="true" closable="true" />

            <p:toolbar style="margin-top: 20px">
                <p:toolbarGroup>
                    <p:button value="Novo" />
                    <p:commandButton value="Salvar" id="botaoSalvar" />
                </p:toolbarGroup>
                <p:toolbarGroup align="right">
                    <p:button value="Pesquisa" />
                </p:toolbarGroup>
            </p:toolbar>

            <p:panelGrid columns="2" id="painel"
                style="width: 100%; margin-top: 20px" columnClasses="rotulo, campo">
                <p:outputLabel value="Nome do Imóvel" for="imovel" />
                <p:inputText id="imovel" size="20" maxlength="20" />

                <p:outputLabel value="Descrição do Imóvel" for="descimovel" />
                <p:inputText id="descimovel" size="60" maxlength="80" />

                <p:outputLabel value="Categoria" for="categoria" />
                <p:selectOneMenu id="categoria">
                    <f:selectItem itemLabel="Selecione a categoria" />
                </p:selectOneMenu>

                <p:outputLabel value="Subcategoria" for="subCategoria" />
                <p:selectOneMenu id="subCategoria">
                    <f:selectItem itemLabel="Selecione a subcategoria" />
                </p:selectOneMenu>

                <p:outputLabel value="Valor unitário" for="valorUnitario" />
                <p:inputText id="valorUnitario" size="10" maxlength="10"
                    value="#{cadastroImovelBean.imovel.valorImovel}" />


            </p:panelGrid>
        </h:form>
    </ui:define>
</ui:composition>

That’s the piece of code I’m interested in:

<p:outputLabel value="Valor unitário" for="valorUnitario" />
<p:inputText id="valorUnitario" size="10" maxlength="10"
    value="#{cadastroImovelBean.imovel.valorImovel}" />

Why didn’t you validate my field?

  • have you tried processing the field? Type <p:ajax Event="change" process="@this" /> in your input.

  • this information is not very clear to me, if you want you can post as an answer, it is more interesting because you will be able to explain your answer in a clearer way.

  • In short, when you click the Include button it saves the data in the database without validating?

  • sorry, I still don’t understand.

  • Do you have a save button right? when you click on it the field is not validated? From what I saw your save button is not connected to any event to save the data in the database.

  • #Rafael’s suggestion helps to validate only one field, and I only want to validate one field for the test question, but in practice it will have to validate all the other fields I am creating, I did a test using the Tomcat server, and it worked normally. The server I was using was Jboss’s wildFly, there must be a different way to use, but I don’t believe that’s the approach that was mentioned.

Show 1 more comment

1 answer

1


To process all fields of your panel you need a button.

<p:commandButton value="Salvar" process="painel @this" update="painel"/>

With this it processes your dashboard and does the validation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.