What is the life cycle behavior of JSF 2 when validating mandatory fields?

Asked

Viewed 290 times

1

I have a customer registration form with two fields, the first field is the CPF with mandatory and the second Name with optional. When editing a client with CPF and Name populated, delete the two fields and click Save, the JSF returns the message "The CPF field is required". However, when presenting the message the CPF field comes blank as it was submitted. The Name field does not come empty as submitted, but with the previous value.

Is this the standard behavior of JSF 2? Can I base this behavior on the JSF JCP documentation? What arguments do you present to the client that the CPF field comes blank to show the requirement and the Name field is restored because the JSF lifecycle has not completed to accept the new value?

2 answers

0

This is the JSF validation lifecycle: inserir a descrição da imagem aqui

Here is an example of a registration panel:

<h:form>
        <h:panelGrid id="painelCadastro">
            <h:outputLabel value="CPF:"/>
            <h:inputText value="#{bean.cpf}" required="true"/>

            <h:outputLabel value="Nome:"/>
            <h:inputText value="#{bean.nome}"/>

        </h:panelGrid>
        <p:commandButton value="Salvar" process="painelCadastro" actionListener="#{seu_metodo_de_validar_ou_salvar}"/>
</h:form>

In this form the only validation is whether the field is required, what I recommend you do is a validation method for your object, where it will validate all your fields by checking if it is null or empty. Your problem may be the lack of processing the new data for the Bean variables, with my example it will always process the values and will do the JSF validation.

  • Rafael, thank you for your answer but the problem is in restoring the value by overwriting the new name field value when the CPF field is validated.

0


I found Balusc’s answer very enlightening about this problem here. He says:

When at least one input component is invalid, after the validation phase, then JSF will not update the model values for any of the input components. JSF will proceed directly to render response phase.

Browser other questions tagged

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