7.5.3 Flow Execution Events 2.3.8

See the light - agile, industrial strength, rapid web application development made easy

(Quick Reference) # 7.5.3 Flow Execution Events - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 2.3.8 ## 7.5.3 Flow Execution Events In order to transition execution of a flow from one state to the next you need some way of trigger an event that indicates what the flow should do next. Events can be triggered from either view states or action states. #### Triggering Events from a View State As discussed previously the start state of the flow in a previous code listing deals with two possible events. A `checkout` event and a `continueShopping` event:
    def shoppingCartFlow = {
        showCart {
            on("checkout").to "enterPersonalDetails"
            on("continueShopping").to "displayCatalogue"
        }
        …
    }
Since the `showCart` event is a view state it will render the view `grails-app/book/shoppingCart/showCart.gsp`. Within this view you need to have components that trigger flow execution. On a form this can be done use the submitButton tag:
    <g:form>
        <g:submitButton name="continueShopping" value="Continue Shopping" />
        <g:submitButton name="checkout" value="Checkout" />
    </g:form>
The form automatically submits back to the `shoppingCart` flow. The name attribute of each submitButton tag signals which event will be triggered. If you don't have a form you can also trigger an event with the link tag as follows:
    <g:link event="checkout" />
Prior to 2.0.0, it was required to specify the controller and/or action in forms and links, which caused the url to change when entering a subflow state. When the controller and action are not specified, all url's are relative to the main flow execution url, which makes your flows reusable as subflows and prevents issues with the browser's back button. #### Triggering Events from an Action To trigger an event from an `action` you invoke a method. For example there is the built in `error()` and `success()` methods. The example below triggers the `error()` event on validation failure in a transition action:
    enterPersonalDetails {
       on("submit") {
             def p = new Person(params)
             flow.person = p
             if (!p.validate()) return error()
       }.to "enterShipping"
       on("return").to "showCart"
    }
In this case because of the error the transition action will make the flow go back to the `enterPersonalDetails` state. With an action state you can also trigger events to redirect flow:
    shippingNeeded {
       action {
           if (params.shippingRequired) yes()
           else no()
       }
       on("yes").to "enterShipping"
       on("no").to "enterPayment"
    }
| Quick Reference (hide)