Search code examples
grailsdropdown

populate a dropdown in grails


i have a user that i give a role to it, and i want when i create the user, to show the roles in a dropdown, then i can select the role that i want to give to him, how can i do it ?? please i really need help in it, cause im new in grails here is the user.groovy

class User {

transient securiteService

String username
String password
String nom
String prenom
String email
String tel

static hasMany = [roles : Role]

static constraints = {
    username blank: false, unique: true
    password blank: false,display: false
    nom nullable: true
    prenom nullable: true
    email email:true, nullable:true
    tel nullable:true, maxSize:20,  matches:/[\+]{0,1}[0-9\s]{3,15}/
}

static mapping = {
    password column: '`password`'
    sort nom: "asc"
    affectations sort : "dateAffectation", order:"desc"
    intervention sort : "responsable", order:"desc"
}
}

Solution

  • The following example will help you, make necessary changes in your code according to your need. For example you have a table / domain Role

    class Role {
       String roleId
       String roleName  // e.g ROLE_ADMIN
    }
    

    // Populate the dropdown in your GSP view ( this will populate all the roles present in the Role table)

    <g:select from="${Role.list().roleName}" name="selectUserRole"
                                    optionKey=""
                                    optionValue=""/>