You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Robin Sander <ro...@gmx.net> on 2009/07/02 19:30:40 UTC

Can a RadioChoice display input for null value?

Hello,

is it possible to get RadioChoice behave more like DropDownChoice  
concerning a null value?
Consider a RadioChoice defining a search parameter, e.g. two values  
'male' and 'female' and a third one
meaning "doesn't matter".
With a DropDownChoice I could easily use setNullValid(true) and then  
define a property <fieldID>.nullValid
which is them used for the null value.
Looking into the source it seems that DropDownChoice always adds  
getDefaultChoice() which does the
magic while RadioChoice does not. Can anyone elaborate on this topic?  
Is there a workaround?

Background is that I have dozens of enums with property files for  
localization and a self-written
EnumChoiceRenderer which does the localization. So if I have a  
property of (enum) type Gender
which defines Gender.MALE and Gender.FEMALE I can't easily define a  
special "NULL" value
without modifying the enum.
So the only option I see would be to really pass null to the  
RadioChoice or to define a special
enum (like GenderWithNull) for any enum I want to use in a RadioChoice.

regards,

Robin.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Can a RadioChoice display input for null value?

Posted by Robin Sander <ro...@gmx.net>.
To answer my own question in case anybody else has the same problem:

It is not possible out of the box. You have to pass null together with  
the other options to RadioChoice
and then set your own ChoiceRenderer which returns the localized value  
in the same way as
AbstractSingleSelectChoice.getDefaultChoice() does for DropDownChoice.
To achieve this I pass a reference to the component to the renderer  
and then return the localized
value in getDisplayValue(), e.g like this:

String getDisplayValue(Object value) {
   if (value != null) {
      return value.toString();
   } else {
     String result =  
component.getLocalizer().getStringIgnoreSettings(component.getId() +  
".nullValid", component, null, null);
     if (Strings.isEmpty(result)) {
	result = component.getLocalizer().getString("nullValid", component,  
"");
     }
     return result;
   }
}

In order to get a pre-selected null value you have to return  
"-1" (AbstractSingleSelectChoice#NO_SELECTION_VALUE)
in getIdValue().

This is kind of a hack for several reasons:
- code from AbstractSingleSelectChoice.getDefaultChoice() is duplicated.
- some magic constants are re-used but unfortunately there are no  
constants in Wicket for them
   (e.g. ".nullValid" or "-1" for the null ID) or they are not public.
- the renderer keeps a reference to the component it is attached to



On 02.07.2009, at 19:30, Robin Sander wrote:

>
> Hello,
>
> is it possible to get RadioChoice behave more like DropDownChoice  
> concerning a null value?
> Consider a RadioChoice defining a search parameter, e.g. two values  
> 'male' and 'female' and a third one
> meaning "doesn't matter".
> With a DropDownChoice I could easily use setNullValid(true) and then  
> define a property <fieldID>.nullValid
> which is them used for the null value.
> Looking into the source it seems that DropDownChoice always adds  
> getDefaultChoice() which does the
> magic while RadioChoice does not. Can anyone elaborate on this  
> topic? Is there a workaround?
>
> Background is that I have dozens of enums with property files for  
> localization and a self-written
> EnumChoiceRenderer which does the localization. So if I have a  
> property of (enum) type Gender
> which defines Gender.MALE and Gender.FEMALE I can't easily define a  
> special "NULL" value
> without modifying the enum.
> So the only option I see would be to really pass null to the  
> RadioChoice or to define a special
> enum (like GenderWithNull) for any enum I want to use in a  
> RadioChoice.
>
> regards,
>
> Robin.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org