History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GRAILS-364
Type: Improvement Improvement
Status: Open Open
Priority: Major Major
Assignee: Graeme Rocher
Reporter: Guillaume Laforge
Votes: 6
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Grails

Add an empty option to g:select and the associated controller logic

Created: 13/Oct/06 01:02 AM   Updated: 05/Sep/07 04:55 PM
Component/s: Controllers, TagLib
Affects Version/s: 0.2.2
Fix Version/s: 1.1

Time Tracking:
Not Specified


 Description  « Hide
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?



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Marc Palmer - 05/Jan/07 07:38 AM
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.


Marc Palmer - 07/Mar/07 07:02 AM
Since 0.4 we have had the noSelection attribute on g:select so this can be implemented quite easily now I think