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

1

I have a customer registration form with two fields, the first field is the CPF with mandatory completion and the second Name with optional fill. When editing a customer with CPF and Name filled in, delete both fields and click Save, JSF returns the message "CPF field is required". However, when displaying the message the CPF field is blank as submitted. The Name field is not empty as it was submitted, but with the previous value.

Would this be the default behavior of JSF 2? Can I base this behavior on the JSF JCP documentation? What arguments do you present to the customer that the CPF field is blank to show the requirement and the Name field is restored because the JSF lifecycle did not complete to accept the new value?     

asked by anonymous 28.09.2015 / 13:53

2 answers

0

I found BalusC's answer very enlightening on this problem here . It says:

  

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

    
28.09.2015 / 20:26
0

This is the validation life cycle in JSF:

Hereisanexampleofaregistrationpanel:

<h:form><h:panelGridid="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 if the field is required , what I recommend you do is a validation method for your object, where you will validate all your fields by checking whether 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.

    
28.09.2015 / 14:54