Reputation: 3296
This is on Grails! This is a very basic thing which apperatnly I am failing to understand.
I have this in my index.gsp
<g:form name="testForm" url="[controller:'test',action:'index']">
<g:textField name="Input A" value="${Input1}"> </g:textField>
<g:textField name="Input B" value="${Input2}"> </g:textField>
</g:form>
I also have this in my TestController class:
class TestController {
def index = {
def Input1
def Input2
}
}
I want to get the two inputs that the user enters on the webpage and save them to the appropriate fields (Input1, Input2) on the controller.
How do I go about it?
thanks
Upvotes: 2
Views: 8445
Reputation: 1
class TestController {
def index = {
def Input1 = params.input1
def Input2 = params.input2
render(Input1+"")
render(Input2+"")
}
}
Upvotes: -2
Reputation: 14197
You can write your form like this:
<g:form name="testForm" controller="test" action="index">
<g:textField name="Input1" value="${Input1}"> </g:textField>
<g:textField name="Input2" value="${Input2}"> </g:textField>
<g:actionSubmit value="Send to controller" action="index"/>
</g:form>
Note that in this case,
Then in the controller
class TestController {
def index = {
def Input1 = params.Input1
def Input2 = params.Input2
["Input1": Input1, "Input2": Input2]
}
}
With this, the values will be rendered properly (inside the returned model)
Upvotes: 6
Reputation: 3296
So having a submit button works.
<g:form name="testForm" controller="test" action="index">
<g:textField name="input1" value="${input1}"> </g:textField>
<g:textField name="input2" value="${input2}"> </g:textField>
<g:submitButton name="Submit" value="Submit"></g:submitButton>
</g:form>
...
class TestController {
def index = {
def Input1 = params.input1
def Input2 = params.input2
render(Input1+"<br />")
render(Input2+"<br />")
}
}
Upvotes: 0
Reputation: 8109
You receive the form parameters from the implicit variable "params
". Do a log.error(params)
in your controller and you will know how they are passed. You can access your parameter like
params."Input 1"
.
Note that there are neat ways to handle multiple inputs from one class, e.g. given a domain class:
class Test {
String a;
String b;
}
You can have a form:
<g:form name="testForm" controller="test" action="index">
<g:textField name="test.a" value="${Input1}"> </g:textField>
<g:textField name="test.b" value="${Input2}"> </g:textField>
</g:form>
And in the controller you do:
class TestController {
def index = {
def testInstance = new Test(params.test)
}
}
However this trick you should only use in admin areas or something, since there are some security considerations to be done.
Upvotes: 1