Grails - Simple hasMany Problem - Using CheckBoxes rather than HTML Select in create.gsp
Asked Answered
M

4

7

My problem is this: I want to create a grails domain instance, defining the 'Many' instances of another domain that it has. I have the actual source in a Google Code Project but the following should illustrate the problem.

class Person {
  String name
  static hasMany[skills:Skill]

  static constraints = {
   id (visible:false)   
   skills (nullable:false, blank:false)
  }
}

class Skill {
  String name
  String description

  static constraints = {
   id (visible:false)   
   name (nullable:false, blank:false)
   description (nullable:false, blank:false)
  }
}

If you use this model and def scaffold for the two Controllers then you end up with a form like this that doesn't work;

Scaffolding

My own attempt to get this to work enumerates the Skills as checkboxes and looks like this;

Custom Create.gsp

But when I save the Volunteer the skills are null!

Failed to save Skills

This is the code for my save method;

def save = {
    log.info "Saving: " + params.toString()
    def skills = params.skills
    log.info "Skills: " + skills 
    def volunteerInstance = new Volunteer(params)
    log.info volunteerInstance
    if (volunteerInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'volunteer.label', default: 'Volunteer'), volunteerInstance.id])}"
        redirect(action: "show", id: volunteerInstance.id)
        log.info volunteerInstance
    }
    else {
        render(view: "create", model: [volunteerInstance: volunteerInstance])
    }
}

This is my log output (I have custom toString() methods);

2010-05-10 21:06:41,494 [http-8080-3] INFO  bumbumtrain.VolunteerController  - Saving: ["skills":["1", "2"], "name":"Ian", "_skills":["", ""], "create":"Create", "action":"save", "controller":"volunteer"]

2010-05-10 21:06:41,495 [http-8080-3] INFO  bumbumtrain.VolunteerController  - Skills: [1, 2]

2010-05-10 21:06:41,508 [http-8080-3] INFO  bumbumtrain.VolunteerController  - Volunteer[ id: null | Name: Ian | Skills [Skill[ id: 1 | Name: Carpenter ] , Skill[ id: 2 | Name: Sound Engineer ] ]] 

Note that in the final log line the right Skills have been picked up and are part of the object instance. When the volunteer is saved the 'Skills' are ignored and not commited to the database despite the in memory version created clearly does have the items. Is it not possible to pass the Skills at construction time? There must be a way round this? I need a single form to allow a person to register but I want to normalise the data so that I can add more skills at a later time.

If you think this should 'just work' then a link to a working example would be great.

If I use the HTML Select then it works fine! Such as the following to make the Create page;

<tr class="prop">
<td valign="top" class="name">
  <label for="skills"><g:message code="volunteer.skills.label" default="Skills" /></label>
</td>
<td valign="top" class="value ${hasErrors(bean: volunteerInstance, field: 'skills', 'errors')}">
    <g:select name="skills" from="${uk.co.bumbumtrain.Skill.list()}" multiple="yes" optionKey="id" size="5" value="${volunteerInstance?.skills}" />
</td>
</tr>   

But I need it to work with checkboxes like this;

<tr class="prop">
<td valign="top" class="name">
  <label for="skills"><g:message code="volunteer.skills.label" default="Skills" /></label>
</td>
<td valign="top" class="value ${hasErrors(bean: volunteerInstance, field: 'skills', 'errors')}">
    <g:each in="${skillInstanceList}" status="i" var="skillInstance">   
      <label for="${skillInstance?.name}"><g:message code="${skillInstance?.name}.label" default="${skillInstance?.name}" /></label>
                                      <g:checkBox name="skills" value="${skillInstance?.id.toString()}"/>
    </g:each>
</td>
</tr> 

The log output is exactly the same! With both style of form the Volunteer instance is created with the Skills correctly referenced in the 'Skills' variable. When saving, the latter fails with a null reference exception as shown at the top of this question.

Hope this makes sense, thanks in advance!

Gav

Misha answered 10/5, 2010 at 20:11 Comment(0)
B
5

Replace your create.gsp <g:checkbox...> code by:

<g:checkBox name="skill_${skillInstance.id}"/>

Then inside the save action of your controller, replace def volunteerInstance = new Volunteer(params) by :

def volunteerInstance = new Volunteer(name: params.name)
params.each {
  if (it.key.startsWith("skill_"))
    volunteerInstance.skills << Skill.get((it.key - "skill_") as Integer)
}

Should work. (code not tested)

Battologize answered 10/5, 2010 at 23:36 Comment(2)
Lifesaver! This was such a headacheMisha
Small optimization- it.key.startsWith is better than it.key.containsKatzenjammer
F
3

I would reader send id list of your has many elements because this can be easily assigned by default in Grails. Your .gsp should look like:

<g:each in="${skills}" var="skill">
            <input type="checkbox"
                   name="skills"
                   value="${skill?.id}"
          </g:each>

and in your controller you can simply stores the value like this:

person.properties = params
person.validate()
person.save()

It's pretty easy, isn't it? :-)

Fortieth answered 29/11, 2011 at 16:51 Comment(1)
If your form contains some error, your solution will not work. You must check whether your command - person - contains each element of the reference collection - skills - and, if so, mark your checkbox as checked. Also, calling save method on a domain class will call validate first, so you can also use save to validate your domain classes.Mercurous
M
3

Grails does not provide data-binding support when you use a checkbox and you want to bind ToMany associations. At least, up to version 2.2.0

Workaround ?

1º option - Write gsp code which behaves like a select component

<g:each var="skillInstance" in="${skillInstanceList}">
    <div class="fieldcontain">
        <g:set var="checked" value=""/>
        <g:if test="${volunteerInstance?.skills?.contains(skillInstance)}">
            <input type="hidden" name="_skills" value="${skillInstance?.id}"/> 
            <g:set var="checked" value="checked"/>
        </g:if>
        <label for="${skillInstance?.name}">
            <g:message code="${skillInstance?.name}.label"
                       default="${skillInstance?.name}" />
        </label>
        <input type="checkbox" name="skills" value="${skillInstance?.id}"
               ${checked} /> 
    </div>
</g:each>

Create your own TagLib

/**
  * Custom TagLib must end up with the TagLib suffix
  *
  * It should be placed in the grails-app/taglib directory
  */
class BindingAwareCheckboxTagLib {

    def bindingAwareCheckbox = { attrs, body ->
        out << render(
                  template: "/<TEMPLATE_DIR>/bindingAwareCheckboxTemplate.gsp",
                  model: [referenceColletion: attrs.referenceColletion,
                          value:attrs.value])
    }

}

Where <TEMPLATE_DIR> should be relative to the /grails-app/views directory. Furthermore, templates should be prefixed with _.

Now you can use your custom TagLib as follows

<g:bindingAwareCheckbox
      referenceCollection="${skillInstanceList}"
      value="${volunteerInstance?.skills}"/>

Once done, binding process will occur automatically. No additional code needed.

Mercurous answered 12/1, 2013 at 22:47 Comment(3)
That's a nice workaround. Note that the binding only works automatically if the relationship is set up with hasMany -- if it is a simple List<Skill> skills the binding won't be done for you.Amah
@elias Yes, you can. I do not wait for Grails when i can create my own workaround. Use BindEventListener and a Function object from Google Guava to convert the request to you List. See stackoverflow.com/a/13538222 and docs.guava-libraries.googlecode.com/git-history/release/javadoc/…Mercurous
Sorry, I meant it wasn't done automatically for you, not that it couldn't be done. But it's cool to see that it's possible to customize the binding stuff even further, wasn't aware of that -- thanks for the pointers. =)Amah
H
0

GSP

 <g:checkBox name="skills" value="${skillInstance.id}" checked="${skillInstance in volunteerInstance?.skills}"/>

Groovy

def volunteerInstance = new Volunteer(params).save()     
def skills = Skill.getAll(params.list('skills')) 
     skills.each{ volunteerInstance.addToSkills(it).save() }
Hardboiled answered 13/3, 2013 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.