(Quick Reference)

6.5.5 Data Binding and Validation - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

6.5.5 Data Binding and Validation

In the section on start and end states, the start state in the first example triggered a transition to the enterPersonalDetails state. This state renders a view and waits for the user to enter the required information:
En la seccion de start and end states, el estado de inicio en el primer ejemplo dispara un transicion hacia el estado enterPersonalDetails. Este estado renderea la vista y espera a que el usuario introduzca la informacion requerida:

enterPersonalDetails {
   on("submit").to "enterShipping"
   on("return").to "showCart"
}

The view contains a form with two submit buttons that either trigger the submit event or the return event:
La vista contiene una forma con dos botones de enviar que cualquiera de los dos dispara el evento de enviar o el evento de retorno:

<g:form action="shoppingCart">
    <!-- Other fields -->
    <g:submitButton name="submit" value="Continue"></g:submitButton>
    <g:submitButton name="return" value="Back"></g:submitButton>
</g:form>

However, what about the capturing the information submitted by the form? To to capture the form info we can use a flow transition action:
Sin embargo, ¿que hay de capturar la informacion enviada por la forma? Para obtener la informacion de la forma podemos usar una accion de flujo de transaccion:

enterPersonalDetails {
   on("submit") {
      flow.person = new Person(params)
      !flow.person.validate() ? error() : success()
   }.to "enterShipping"
   on("return").to "showCart"
}

Notice how we perform data binding from request parameters and place the Person instance within flow scope. Also interesting is that we perform validation and invoke the error() method if validation fails. This signals to the flow that the transition should halt and return to the enterPersonalDetails view so valid entries can be entered by the user, otherwise the transition should continue and go to the enterShipping state.
Note como se desarrolla el data binding desde los parametros de la peticion y coloca la instancia Person dentro del alcance flow. Tambien es interesante que desarrollamos validation e invocamos el metodo error() si la validacion falla. Esto avisa al flujo que la transicion debe ser detenida y regresar a la vista enterPersonalDetails y asi entradas validas pueden ser introducidas por el usuario, de otra manera la transicion debe continuar e ir al estado enterShipping.

Like regular actions, flow actions also support the notion of Command Objects by defining the first argument of the closure:
Como las acciones regulares, las acciones de flujo soportan la nocion de Command Objects definiendo el primer agurmento del closure:

enterPersonalDetails {
   on("submit") { PersonDetailsCommand cmd ->
       flow.personDetails = cmd
      !flow.personDetails.validate() ? error() : success()
   }.to "enterShipping"
   on("return").to "showCart"
}