The "required=true" flag means that the user *must supply a value*, but
doesn't ensure that they provide a specific value. With a checkbox, the
user *always* effectively provides a value: unchecked=false, checked=true.
So as Martin says, required=true is in fact pointless for checkboxes.
And his recommendation to just use the validator *attribute* on the
checkbox component is probably the best, although a custom validator
class would also be possible. In either case, it should take about 10
lines of code.
Cheers, Simon
Martin Marinschek schrieb:
> Hi Michael,
>
> you are on the right track - the required==true flag will not help in
> the case of the checkbox, as for checkboxes, JSF will _always_ set a
> value (either true or false, due to the problems you mentioned above -
> there is no way to distinguish between false and value not set at
> all...).
>
> what you can do instead is set the validator="#{myBb.validator}"
> attribute and validate in this method accordingly (or, as you said,
> write a new validator).
>
> regards,
>
> Martin
>
> On 2/27/08, Michael Heß <[EMAIL PROTECTED]> wrote:
>
>> Hi List,
>>
>> now that I have spent several hours trying to figure it out on my own, I'm
>> giving this list a try.
>>
>> What I want to do is rather basic: I want to have a mandatory checkbox in
>> my view. So if the user wants to continue working with the application he
>> must first tick the box.
>>
>> So what I came up with was to use the required="true" on the
>> h:selectBooleanCheckbox . Unfortunately this does not work. As I tried to
>> find out what goes wrong I gave h:inputText a try, and it worked as
>> expected, i.e. the form was considered invalid as long as the textbox was
>> empty. So something is fishy about the checkbox...
>>
>> OK, so I remember back from the struts days, that checkboxes are tricky
>> because they do not submit any value at all (not even an empty one) when
>> unchecked. Searching some more, I found a rather interesting discussion in
>> http://issues.apache.org/jira/browse/MYFACES-1467. Although the main
>> problem is about validators not running, the issue that required="true"
>> gets ignored in case the component does not create a http parameter at
>> all, is discussed at well. To the end of that issue it first seemed like a
>> patch was supplied, but then again several problems popped up, and someone
>> suspects that it's a spec issue.... frankly - i`m at a loss here. :-)
>>
>> So, what is the right approach for this behaviour? Create a
>> "MandatoryCheckboxValidator" and attach it to the checkboxes? Maybe
>> someone else had the problem already and can give me a hint how to
>> proceed?
>>
>