Announcement Announcement Module
Collapse
No announcement yet.
Dynamic default value Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      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...

      Comment

      Working...
      X