user3463332
user3463332

Reputation: 21

g:checkbox does not save checked items individually for list items

I am relatively new to grails and have been trying to configuring a check box to set a boolean value to true when it is checked for a number of list items.

The relationship is a hasMany between two domains where the boolean canEdit is set in the work domain. The other domain is called role. I have set up the relationship so that work hasManyRoles and I would like my domain to reflect the boolean canEdit being set to true for some of these roles based on what gets checked in the checkbox list.

I have managed to get the gsp working displaying the list with checkboxes beside each one but am unsure as to how to save the boolean as true once some of these roles are checked as currently they are all getting set to true regardless of which is checked.

Any assistance would be greatly appreciated. I will post my code below:

class Work {

    static hasMany = [canEditRole: Role]

    String name = "Default"
    Boolean canEdit = false

static constraints = {
        name blank: false, unique: true
        canEdit nullable: true
}


  boolean canEdit(Role role){ // not sure the best way to check for if the role is associated, this was something I tried with te default add method.
      if(canEdit){
          addToCanEditRole(role)
      }
      return canClose
  }
}

class WorkController{

    def save(Long id, String canEdit) {
        def work = Work.get(params.id)
        def roleToCheck = Role.findByName(params.roleNames)
        Boolean roleCanEdit = canEdit ? true : false
        if(roleCanEdit){
            workflow.canEdit(roleToCheck)
            println "role is " + roleToCheck
        }else{
        println "no roles added"
        }   
    }
}

GSP Code Snippet:

<tr class="prop">
                    <td valign="top" class="name">
                        <label for="canEdit">Allow Role to Edit</label>
                    </td>
                    <td valign="top" style="text-align: left;"
                        class="value ${hasErrors(bean: WorkInstance, field: 'canEdit', 'errors')}">
                        <ul>
                            <g:each in="${roleNames}" var="role">
                                <li>
                                    <g:checkBox name="canEdit" value="${WorkInstance?.canEdit}"/> <label>${role.name}</label>
                                </li>
                            </g:each>
                        </ul>
                    </td>
                </tr>

                </tbody>
            </table>
        </div>

        <div class="buttons">
            <span class="button"><g:actionSubmit class="save" action="save"
                                                 params="[roleName: roleNames.name]"
                                                 value="${message(code: 'default.button.update.label', default: 'Update')}"/></span>
        </div>

Upvotes: 0

Views: 239

Answers (1)

renz
renz

Reputation: 1070

Have you looked at grails.plugin.springsecurity.ui.UserController ? It seems that the functionality that you want is already there.

protected Map buildUserModel(user) {

    Set userRoleNames = user[authoritiesPropertyName].collect { it[authorityNameField] }
    def granted = [:]
    def notGranted = [:]
    for (role in sortedRoles()) {
        String authority = role[authorityNameField]
        if (userRoleNames.contains(authority)) {
            granted[(role)] = userRoleNames.contains(authority)
        }
        else {
            notGranted[(role)] = userRoleNames.contains(authority)
        }
    }

    [roleMap: granted + notGranted, tabData: tabData, user: user]
}

Then in your gsp, you can access roleMap like:

                <g:each var="entry" in="${roleMap}">
                    <div class="form-group">
                        <label class="col-xs-4">
                            <g:link controller='role' action='show' id='${entry.key.id}'>
                                ${entry.key.authority.encodeAsHTML()}
                            </g:link>
                        </label>
                        <div class="col-xs-8">
                            <g:checkBox name="${entry.key.authority}" value="${entry.value}"/>
                        </div>
                    </div>
                </g:each>

Upvotes: 0

Related Questions