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

梦想的初衷 提交于 2020-01-02 02:47:09

问题


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;

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

But when I save the Volunteer the skills are null!

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


回答1:


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)




回答2:


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? :-)




回答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.




回答4:


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() }


来源:https://stackoverflow.com/questions/2806101/grails-simple-hasmany-problem-using-checkboxes-rather-than-html-select-in-cr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!