tags:

views:

61

answers:

2

I have a DropDownChoice component on my Form and when the form is submitted and nothing is selected from the DropDownChoice, the default value that's returned is "-1". Is there are a way to change this behavior?

+1  A: 

Hook to the beforeSubmit, and change what you want. I think it is good to have such a value, Since first element is 0, notting means -1.

nerkn
+2  A: 

This behavior is controlled by a constant field

protected static final String NO_SELECTION_VALUE = "-1";

in AbstractSingleSelectChoice, which is a superclass of DropDownChoice.

You can't change this value in a subclass, so in order to change the value used, you would have to locate uses of this constant and override the methods that use it to use some other default.

Doing that would be risky, though it's likely possible. I know you'd have to override at least getDefaultChoice(final Object selected) and getModelValue().

Why do you wish to do this? It's a sensible value for its purpose.

Perhaps there's a better approach to accomplishing your underlying need.

Don Roby
I wanted to know why this happens/what is behind it. I don't use indexes - each option is mapped to a record in the database and I've overridden the getIdValue(Object, Int) method, so that it returns ID of the corresponding object. The id's shouldn't be negative, but since they're of type Long, hypothetically, it could happen. That's why I'd prefer if I could return null when nothing is selected.
John Manak
@John - If this is a real worry, it's more easily handled by modifying the getIdValue to return a modified ID that covers distinguishing the value. Since getIdValue returns a String, this is easily accomplished just by appending something to the ID.
Don Roby