Reputation: 685
It gives me an error while submitting multiple values in Database Error: IndexOutofBond
<form action="emp/forsubmit" method=" post">
<input id="emp[0].name" name="emp[0].name" type="text"/>
<input id="emp[0].lastname" name="emp[0].lastname" type="text"/>
<input id="emp[1].name" name="emp[1].name" type="text"/>
<input id="emp[1].lastname" name="emp[1].lastname" type="text"/>
</form>
I am using MongoDb as Db
Controller Source Code :
def update(){
if(empInstance.getEmp_history() == null || empInstance.getEmp_history().size() == 0) {
empInstance.setEmp_history(new ArrayList<EmpHistory>());
empInstance.getEmp_history().add(new EmploymentHistory());
}
empInstance.properties = params
}
Upvotes: 0
Views: 1533
Reputation: 1
Use the same name attribute value:
First: <input type="text" name="firstname"/>
First: <input type="text" name="lastname"/>
Second: <input type="text" name="firstname"/>
Second: <input type="text" name="lastname"/>
And the code below work even the element is only one and they will be processed as an array by grails when form has been submitted, and then populate your domain object for every loop round :
def firstName = params.list('firstname')
def lastName = params.list('lastname')
firstName.eachWithIndex { value, index ->
// use index to access the values in other array
// params.lastname[index]
def mydomain = new MyDomain()
mydomain.firstname = value
mydomain.lastname = lastName[index]
mydomain.save()
}
Upvotes: 0
Reputation: 508
Use the same name attribute value:
First: <input type="text" name="firstname"/>
First: <input type="text" name="lastname"/>
Second: <input type="text" name="firstname"/>
Second: <input type="text" name="lastname"/>
And they will be processed as an array by grails when form has been submitted, and then populate your domain object for every loop round :
params.firstname.eachWithIndex { value, index ->
// use index to access the values in other array
// params.lastname[index]
def mydomain = new MyDomain()
mydomain.firstname = value
mydomain.lastname = params.lastname[index]
mydomain.save()
}
Upvotes: 2