Dynamic default value

mvanhouten

New member
Hi,

I am currently developing my first Grails project. It might be a stupid question but how do I set a default value for a column within the create view or domain or controller? I know I can set a static value within the domain like this
Code:
String foo = "Foo"
in this case however I want to retrieve a value from a different domain. To make it more precise - I am using the nimble plugin and would like to set the default value of a user column to the current user....

Can anybody give me some input on this issue?

Millhouse
 

dave

Administrator
Staff member
If what I understand is that you wish to set default properties of a newly created domain object with data obtained from another domain object then I think I personally would take the approach of getting & setting the initial data in whichever controller action is responsible for first creating a new instance of the domain object. E.g. 'create' if thats what you are using.
 

mvanhouten

New member
If what I understand is that you wish to set default properties of a newly created domain object with data obtained from another domain object then I think I personally would take the approach of getting & setting the initial data in whichever controller action is responsible for first creating a new instance of the domain object. E.g. 'create' if thats what you are using.

Hi Dave,

thanks for your help!
What you understood and described above is what I intend to implement.... Could you possibly give me some example code for clarification? Originally I wanted to set the default value within the create view like this:

Code:
<g:form action="save" >
                <div class="dialog">
                    <table>
                        <tbody>
                            ....

                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="user"><g:message code="timeperiod.user.label" default="User" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: timeperiodInstance, field: 'user', 'errors')}">
                                    <g:textField name="user" [B]value="${user.username}[/B]" />  // <-- Set the default value 
                                </td>
                            </tr>
                        
                        </tbody>
                    </table>
                </div>
In this specific case (using the Nimble plugin) Nimble even provides a tag
HTML:
<n:principalName/>
which does exactly what I want. Of course this tag cannot be used within the form....

Millhouse
 

dave

Administrator
Staff member
Well the first thing that comes to mind is whether you want the user to be able to specify a different username or whether you are wanting to force the value to something specific?

If you do want the user to be selectable then perhaps a g:select is better choice of tag so you only allow usernames which exist in your database to be selected.

If you didn't want the user to be optional, then don't use a textfield, just output the username and either include a g:hidden tag to hold the username or set it within the 'save' action.

Unfortunately I haven't used nimble so can't really help too much but it sits on top of Shiro doesn't it so if you wanted to get and set the user to your new domain object during the save action then surely something along the lines of

def user = User.get(SecurityUtils.getSubject()?.getPrincipal())

myOtherObject.user = user

should work
 

mvanhouten

New member
Dave,

Thanks again for your help - I really do appreciate it!

I also will try what you've recommended.

For now I implemented it within the view like this...

Code:
<%@ page import="org.apache.shiro.SecurityUtils" %>
.
.
.
.
                    <table>
                        <tbody>
			. 
			. 
			. 
			.
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="user"><g:message code="timeperiod.user.label" default="User" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: timeperiodInstance, field: 'user', 'errors')}">
                                    <g:select name="user.id" from="${User.list()}" optionKey="id" value="${SecurityUtils.subject?.principal}"  />
                                </td>
                            </tr>
                        </tbody>
                    </table>
It does pretty much what I wanted...
 
Top