Details

    • Type: Sub-task Sub-task
    • Status: Closed
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: 0.2.2
    • Fix Version/s: 1.1-beta3
    • Component/s: Controllers, TagLib
    • Labels:
      None

      Description

      When an object from a class Foo is associated with a Bar object, we don't always want to have the association.
      So when in the view we've got a select which shows all Bar instances, an empty option would be good to.

      Example use case from Fred Janon:

      I have an "Shift" domain class that references an "Officer" domain
      class. An "Officer" can be optional assigned to a "Shift". The user can
      change the date/time of the "Shift" and at some point assign an
      "Officer" to the shift. By default Grails builds an "Edit" view for the
      "Shift" with a "Select" referencing the "Officer" list. The problem is
      that the "Select" doesn't have an empty entry for the "Officer". Even
      if the user wants to change only the time of the shift, the first
      officer will be selected by default and saved.

      Is there a way to make the "Officer" field optional in the "Edit Shift"
      view?

      This is the code generate by Grails:

      <g:select optionKey="id" from="$

      {[''] + Officer.list()}"
      name="officer.id" value="${shift?.officer?.id}"></g:select>

      I added an empty entry in the list, it shows correctly in the HTML
      page, but then Hibernate generates an exception when I save the edited
      "Shift"
      <g:select optionKey="id" from="${[''] + Officer.list()}

      "
      name="officer.id" value="$

      {shift?.officer?.id}

      "></g:select>

      Any suggestion to do this cleanly?

        Activity

        Hide
        Marc Palmer added a comment -

        Generally we need the option to have a blank first element anyway, even if it is not a valid value for the field. This applies to date picker and any other drop-down select field.

        Not having a blank item listed leads to false data submission where lazy/unobservant users accept the first selected setting.

        Show
        Marc Palmer added a comment - Generally we need the option to have a blank first element anyway, even if it is not a valid value for the field. This applies to date picker and any other drop-down select field. Not having a blank item listed leads to false data submission where lazy/unobservant users accept the first selected setting.
        Hide
        Marc Palmer added a comment -

        Since 0.4 we have had the noSelection attribute on g:select so this can be implemented quite easily now I think

        Show
        Marc Palmer added a comment - Since 0.4 we have had the noSelection attribute on g:select so this can be implemented quite easily now I think
        Hide
        Graeme Rocher added a comment -

        Fixed in GRAILS-1793

        As mentioned about use use noSelection and then when a property is nullable it will pass validation/data binding

        Show
        Graeme Rocher added a comment - Fixed in GRAILS-1793 As mentioned about use use noSelection and then when a property is nullable it will pass validation/data binding
        Hide
        Main Pal added a comment -

        <g:select from="$

        {[[id:'',fileName:'Select']]+com.vividolabs.mobilebi.Excel.list()}

        " optionKey="id" optionValue="fileName"/>
        Say Excel is a domain and 'id' and 'fileName' are to be accessed in the list, add a list of map having these two fields, in the original list.

        Show
        Main Pal added a comment - <g:select from="$ {[[id:'',fileName:'Select']]+com.vividolabs.mobilebi.Excel.list()} " optionKey="id" optionValue="fileName"/> Say Excel is a domain and 'id' and 'fileName' are to be accessed in the list, add a list of map having these two fields, in the original list.

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            Guillaume Laforge
          • Votes:
            7 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development