GRAILS Form post

I am currently digging into Groovy and Grails and “Beginning Groovy and Grails” is a nice Book to get started. In this post I will add my findings while working with the proposed demo app.

Note, although the source code is available for download, I think that you learn more from actually writing code.

First quirk in the book is the actual form building process in Chapter 5 – Handeling the Login and Logout Action. Here is a snippet of the code that doesn´t actually work:

        <form>
            <span class="nameClear">
                <label for="login">
                    Sign In:
                </label>
            </span>
            <g:select name="userName" from="${User.list()}" optionKey="userName" optionValue="userName"></g:select>
<div class="buttons">
                <span class="button">
                    <g:actionSubmit value="Login" />
                </span></div>
</form>

In order for GRAILS to accept the form post you need to tweak the code in of two ways. Either by adding the action into the actionSubmit tag like so:

       <g:actionSubmit value="Login" action="handleLogin"/>

or use a normal submit button (or leave the actioSubmit Button in place) and swap the form tag for a g:form and put the action in there:

       <g:form action="handleLogin">

Full source code:

       <g:form action="handleLogin" method="post" >
            <span class="nameClear">
                <label for="login">
                    Sign In:
                </label>
            </span>
            <g:select name="userName" from="${User.list()}" optionKey="userName" optionValue="userName"></g:select>
<div class="buttons">
                <span class="button">
                    <g:actionSubmit value="Login" />
                </span></div>
</g:form>

Haven´t checked wether it is possible to do something like:

        <form>
            <span class="nameClear">
                <label for="login">
                    Sign In:
                </label>
            </span>
            <g:select name="userName" from="${User.list()}" optionKey="userName" optionValue="userName"></g:select>
<div class="buttons">
                <span class="button">
                    <g:actionSubmit value="Login" action="handleLogin" />
                    <g:actionSubmit value="DoSomethingElse" action="otherMethod" />
                </span></div>
</form>

This would make it very easy to post data to different controller methods. Right now, can´t think of a practical use case but it´s nifty anyway.

2 comments so far

  1. Uday on

    Still doesn’t do what its supposed to…

    • easyel on

      Worked for me. Check the downloadable code from the apress site. Maybe there is another bug in your code.


Leave a reply