Results 1 to 3 of 3

Thread: Dynamic default value

  1. #1
    Join Date
    Jul 2011
    Posts
    2

    Default Dynamic default value

    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

  2. #2
    Join Date
    Jun 2010
    Location
    London
    Posts
    304

    Default

    I would normally do this in a service with a createNewBar() method. However, you might want to try lazy initialisation:

    Code:
    @Lazy String foo = { return User.get(SecurityUtils.subject.principal) }
    or something like that. A word of warning: I haven't ever tried the @Lazy annotation with domain class properties, so I don't know whether it works or not.

    Depending on the exact behaviour you want, you could also implement a GORM event handler such as beforeInsert() to initialise the property before it is persisted to the database.

  3. #3
    Join Date
    Jul 2011
    Posts
    2

    Default

    Thanks for your help!

    I 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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •