You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@click.apache.org by WarnerJan Veldhuis <wa...@qprcostcontrol.com> on 2009/10/27 10:30:20 UTC

Zero value in Select/Option

Hi,

I have a set of values, 0 to 3 which I put in a Select, which is set to 
be required. But the validate fails for the value zero. It says I must 
select a value. Is that how it's supposed to work?

Cheers,

WarnerJan


Re: Zero value in Select/Option

Posted by Bob Schellink <sa...@gmail.com>.
WarnerJan Veldhuis wrote:
> 
> One might think the other way around: if the Select is required, then my 
> option-list only contains possible values, and not an empty one.


Yeah thats true and in a sense the default behavior. By not adding a "nonselected" value, the Select 
becomes required since it will always have a value. The only difference is that the red '*' is not 
displayed. :)

Re: Zero value in Select/Option

Posted by WarnerJan Veldhuis <wa...@qprcostcontrol.com>.
I tried your suggestion of Option.EMPTY_OPTION and it works.

One might think the other way around: if the Select is required, then my 
option-list only contains possible values, and not an empty one. It's a 
long search if you don't know this specific behaviour. :)

Thanks.


On 10/27/2009 11:11 AM, Bob Schellink wrote:
> Just to be clear, Click will assume that the first Option is the 
> "nonselected" option. If the first Option is selected (doesn't matter 
> what that Option's value is), then Click will raise the validation 
> error. So by adding an initial empty option, all other options are 
> treated as valid.
>
> Bob Schellink wrote:
>> Hi WarnerJan,
>>
>> It seems a bit odd at first but it makes sense. If the only values 
>> one can select are 0,1,2,3 then there is no need to set the Select as 
>> required since the Select will always have a value.
>>
>> If however the Select is set to required, then there should be an 
>> "nonselected" value for the user to choose from. For the Select that 
>> value is an empty string. So normally one would use the 
>> Option.EMPTY_OPTION:
>>
>>   Select select = new Select();
>>   select.setRequired(true);
>>   select.add(Option.EMPTY_OPTION);
>>   select.add(...);
>>
>> Or if you want to display a value for the "nonselected" value, do this:
>>
>>   Select select = new Select();
>>   select.setRequired(true);
>>   select.add(new Option("", "Please select a value..."));
>>   select.add(new Option("M", "Male");
>>   select.add(new Option("F", "Female");
>>
>> See the Select Javadoc for more examples of the required property.
>>
>> kind regards
>>
>> bob
>>
>>
>> WarnerJan Veldhuis wrote:
>>> Hi,
>>>
>>> I have a set of values, 0 to 3 which I put in a Select, which is set 
>>> to be required. But the validate fails for the value zero. It says I 
>>> must select a value. Is that how it's supposed to work?
>>>
>>> Cheers,
>>>
>>> WarnerJan
>>>
>>
>>

Re: Zero value in Select/Option

Posted by Bob Schellink <sa...@gmail.com>.
Just to be clear, Click will assume that the first Option is the "nonselected" option. If the first 
Option is selected (doesn't matter what that Option's value is), then Click will raise the 
validation error. So by adding an initial empty option, all other options are treated as valid.

Bob Schellink wrote:
> Hi WarnerJan,
> 
> It seems a bit odd at first but it makes sense. If the only values one 
> can select are 0,1,2,3 then there is no need to set the Select as 
> required since the Select will always have a value.
> 
> If however the Select is set to required, then there should be an 
> "nonselected" value for the user to choose from. For the Select that 
> value is an empty string. So normally one would use the 
> Option.EMPTY_OPTION:
> 
>   Select select = new Select();
>   select.setRequired(true);
>   select.add(Option.EMPTY_OPTION);
>   select.add(...);
> 
> Or if you want to display a value for the "nonselected" value, do this:
> 
>   Select select = new Select();
>   select.setRequired(true);
>   select.add(new Option("", "Please select a value..."));
>   select.add(new Option("M", "Male");
>   select.add(new Option("F", "Female");
> 
> See the Select Javadoc for more examples of the required property.
> 
> kind regards
> 
> bob
> 
> 
> WarnerJan Veldhuis wrote:
>> Hi,
>>
>> I have a set of values, 0 to 3 which I put in a Select, which is set 
>> to be required. But the validate fails for the value zero. It says I 
>> must select a value. Is that how it's supposed to work?
>>
>> Cheers,
>>
>> WarnerJan
>>
> 
> 


Re: Zero value in Select/Option

Posted by Bob Schellink <sa...@gmail.com>.
Hi WarnerJan,

It seems a bit odd at first but it makes sense. If the only values one can select are 0,1,2,3 then 
there is no need to set the Select as required since the Select will always have a value.

If however the Select is set to required, then there should be an "nonselected" value for the user 
to choose from. For the Select that value is an empty string. So normally one would use the 
Option.EMPTY_OPTION:

   Select select = new Select();
   select.setRequired(true);
   select.add(Option.EMPTY_OPTION);
   select.add(...);

Or if you want to display a value for the "nonselected" value, do this:

   Select select = new Select();
   select.setRequired(true);
   select.add(new Option("", "Please select a value..."));
   select.add(new Option("M", "Male");
   select.add(new Option("F", "Female");

See the Select Javadoc for more examples of the required property.

kind regards

bob


WarnerJan Veldhuis wrote:
> Hi,
> 
> I have a set of values, 0 to 3 which I put in a Select, which is set to 
> be required. But the validate fails for the value zero. It says I must 
> select a value. Is that how it's supposed to work?
> 
> Cheers,
> 
> WarnerJan
>