How to use p:messages from Primefaces?

Asked

Viewed 804 times

3

I defined the field nome using p:inputText to receive values, it is a field obligatory. How do I display a message showing that this field is required, using p:messages? I am also using JSF.

Follows my HTML:

<h:form>
        <p:messages/>

        <p:panelGrid columns="2">
            <f:facet name="header">
                Cadastro de Pessoas
            </f:facet>
            <h:outputLabel value="Nome" for="nome"/>
            <p:inputText id="nome" required="true"/>

            <f:facet name="footer">
                <p:commandButton value="Cadastrar" icon="ui-icon-disk"
                                 iconPos="right"/>
            </f:facet>
        </p:panelGrid>
    </h:form>

Follow the image of the Form:

Formulário

2 answers

4

Have you tried using a p:message only for the component p:inputText?

Would look like this:

<h:form>
    <p:messages/>

    <p:panelGrid columns="2">
        <f:facet name="header">
            Cadastro de Pessoas
        </f:facet>
        <h:outputLabel value="Nome" for="nome"/>
        <p:inputText id="nome" required="true"/>
        <p:message for="nome" />

        <f:facet name="footer">
            <p:commandButton value="Cadastrar" icon="ui-icon-disk"
                             iconPos="right"/>
        </f:facet>
    </p:panelGrid>
</h:form>

I think that way it works.

Hugs.

  • No, unfortunately it didn’t work.

  • What happened after the changes? Failed or did not change anything in the flow?

3

<p:panelGrid columns="2">
    <f:facet name="header">
        Cadastro de Pessoas
    </f:facet>
    <h:outputLabel value="Nome" for="nome"/>
    <p:inputText id="nome" required="true" requiredMessage="O campo nome é obrigatório"/>

    <f:facet name="footer">
        <p:commandButton value="Cadastrar" icon="ui-icon-disk"
                         iconPos="right" action="seuManagedBean.metodo()" update="@form"/>
    </f:facet>
</p:panelGrid>

With the attribute "requiredMessage" Voce can custom the message of each field. If you have 10 required fields and all of them are not filled in, the message of all of them will only appear in p:messages , so your error messages or alert is in one place.

Browser other questions tagged

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