Search code examples
grailsgrails-2.0grails-controller

Grails 2.x submit form with additional parameters


I am a newbie to grails I want to make a form that submit form params plus other params in a gsp

the gsp looks like this:

<g:form url="action:'myAction',controller:'myTerms', params:[params: params, myCheckbox: myCheckbox]">
                    <p><g:checkBox id="myCheckbox" name="myCheckbox" value="true" checked="checked"/><g:message code="terms.agree"/><a style="color: #ed1c24" href="/xyz"><g:message code="terms.termsConditions"/></a><g:message code="terms.ofService"/></p>
                    <g:actionSubmit value="Subscribe" />
</g:form>

it should submit to controller named MyTermsController to the action below:

def myAction(){
        if(/*condition 1*/){
            try{
                //code
                if (/*condition 2*/) {
                    //more code
                }
            }catch (AlreadyPurchasedException ape){
                redirect(controller: "controllerA", action: "showXhtml", params:params)
            }
            redirect(controller: "controllerB", action: "handlePaymentXhtml", params: params)
        }else if(/*condition 3*/){
            redirect(controller:"controllerC", action:"purchaseXhtml", params:params)
        }
    }

What I am trying to do is to submit a form with params without using javascript.

now every time I press submit, the application is redirected to the error page telling me that the page is not found.

can anyone tell me what am I missing/doing wrong?


Solution

  • I think the issue is with your syntax in the form url attribute, you could try the following approach:

    <g:form controller="mTerms" action="myAction">
    

    Also, you don't need to specifically submit params or myCheckbox as they'll be submitted automatically.

    If you wanted to submit additional values you could use the spread operator like this:

    <g:form controller="mTerms" action="myAction" params="[aKey: 'aValue', *:params]">
    

    Finally I think you need to use g:submitButton rather that g:actionSubmit

    <g:submitButton value="Subscribe" name="subscribe" />