How to make a form for adding users, which have roles, I want to use something like in yabe example, but without crud...
User:
@ManyToMany(cascade=CascadeType.ALL)
public Set<Role> roles = new HashSet();
Role:
@ManyToMany(mappedBy="roles")
public Set<User> users = new HashSet<User>();
#{field 'user.email'}
<input id="${field.id}" name= "${field.name}" class="element text" maxlength="255" size="20" value="${field.value}"/>
#{/field}
...
<select multiple name="roles">
#{field 'user.roles'}
<option value="admin">admin</option>
<开发者_运维百科option value="user">user</option>
#{/field}
</select>
There's probably a better way to do it, and I'd be interested in finding out, but I do it like this:
<div class="field">
<select name="user.roles.id" multiple>
%{ models.Role.all().fetch().sort{ it.toString() }.each() { }%
%{ selected = false; user?.roles.each() { f -> if (f.id == it?.id) selected = true; } }%
<option value="${it?.id}"${selected ? 'selected'.raw() : ''}>${it}</option>
%{ } }%
</select>
</div>
精彩评论