Reputation: 12447
I need to create a dynamic form in grails which has various dynamically created input boxes at runtime, which then submits its params to a controller just like a form created at compile time. Any ideas?
The view gsp might look like this:
<g:form controller="report" action="view">
<input type="submit" value="Submit">
<!-- insert some inuput params here at runtime -->
</g:form>
and the action controller would be:
class ReportController {
def view = {
def someDynamicParam = "someParam"
if (params["someParam"] != null) {
...
}
The problem is I have no idea how to render the form input fields in a way that grails will understand them on submission.
Upvotes: 0
Views: 1712
Reputation: 3595
I would do something like
<g:form controller="report" action="view">
<input type="submit" value="Submit">
<g:if test="${checkIfParamsExists}">
<input type="text" id="newParamName"/>
</g:if>
</g:form>
And in the controller side, your code should work fine.
Upvotes: 1
Reputation:
To create dynamic input's you need JavaScript. I suggest you to use some framework to make it easier. Taking JQuery as example:
$('<input>').attr({
type: 'text',
id: 'foo',
name: 'foo'
}).appendTo('form');
Then you need to think how to bind it to a Domain Class. Looking at your question I cannot see what you need, but let's make an example where the user can set values:
//domain class
class MyDomainClass {
String value
}
//javascript to add a new value
$('<input>').attr({
type: 'text',
name: 'values'
}).appendTo('form');
//controller submit action
class MyController {
def save() {
def values = params.list('values') //get the list of values
values.each {
MyDomain instance = new MyDomain(value: it)
instance.save()
}
}
}
The key here is to use the same name
in your html, and to use params.list()
to transform the submited values in a List.
Upvotes: 1