Dropdown/Select list of Grails SpringSecurity property?
-
05-07-2021 - |
Question
I am currently working on a Grails project and would like to know the best way of showing a select list of objects from the Spring Security domain model:
I know how to create a select list but I just want to find out the best way within grails to populate it with the objects from that domain. I was thinking of calling something like "user.list(params)" and then passing that to the view, I could then access the defined object within that domain, but im not 100% sure wether that is the best way or if it can be done that way?
Thanks in advance
EDIT.....
I have the following custom model setup in the controller:
class UserModel {
String username
String firstName
String lastName
def email = User.email.list()
String[] emails = email
static constraints = {
username blank: false
firstName blank: false
lastName blank: false
}
}
Then when the index page for this view is initialized I pass this to it:
[model: new UserModel(copy)]
and finally on the view i have this:
<g:select name="emails" from="${model.emails}"
Now when I run the application I get this error:
No signature of method: grails.plugins.springsecurity.ui.UserModel.propertyMissing() is applicable for argument types: () values: [] Possible solutions: propertyMissing(java.lang.String)
Can anyone please help with this?? Thanks
Solution 2
I have been working hard at this and have implemented a solution for the issue I was having and it all works fine. Below is what I have in my controller for the view in question:
def index = {
[userList: Users(), command: new RegisterCommand(copy)]
}
protected List Users() {
lookupUserClass().list()
}
The List Users calls another function from an extended class which is shown below:
protected Class<?> lookupUserClass() {
grailsApplication.getDomainClass(lookupUserClassName()).clazz
}
This view then renders the list of items that I want to show in a select list:
<g:select name="emails" from="${userList.emails}" />
You can also do this if you are looking for unique values:
<g:select name="emails" from="${userList.emails.unique()}" />
Thanks
OTHER TIPS
You approach looks absolutely correct. In the controller action you populate the model:
def listUsers = {
[ 'users': Users.list( params ) ]
}
and in the view you use the model for populating the listbox:
<g:select name="users" from="${ users }" />
Your issue looks very simple, so I hope, I've got the idea behind the question correctly :-)