You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Michael Heß <mh...@ordix.de> on 2008/02/27 17:56:13 UTC
Checkbox and required=true
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?
Thanks and bye,
Michael
--
Michael Heß
Senior Consultant
ORDIX AG
Westernmauer 12-16
33098 Paderborn
http://www.ordix.de
Tel.: 05251 / 10 63 0
Fax : 05251 / 10 63 99
Mob : 0163 /673 49 43
ORDIX AG - Aktiengesellschaft für Softwareentwicklung, Schulung, Beratung
und Systemintegration
Vorsitzender des Aufsichtsrates: Prof. Dr. Hermann Johannes
Vorstand: Wolfgang Kögler (Vorsitzender), Benedikt Georgi, Christoph
Lafeld, Axel Röber
Firmensitz: Westernmauer 12 - 16, 33098 Paderborn, Tel: 05251 / 1063-0,
Fax: 0180 / 1 67 34 90
Amtsgericht Paderborn, HRB 2941, Ust-IdNr.DE 126333767, Steuernummer:
339/5866/0142
Re: Checkbox and required=true
Posted by Mike Kienenberger <mk...@gmail.com>.
Well, if you're going to be a purist, the proper thing would be to
create a drop-in component subclass of checkbox that only accepted
checked=true input. :-)
But that's overkill. The most simple reusable way to do this is to
create a validator. I have a project, which when eventually
converted to JSF, would need such a validator. Even though it's
simple, I'd say it's a reasonable candidate for the commons validator
project since it's a common use case.
On Wed, Mar 5, 2008 at 3:49 AM, Michael Heß <mh...@ordix.de> wrote:
> Thanks for you input.
>
> I gave this some more thought on my way home last night. When I think
> about it - is a validator the right place to do this kind of "business
> logic" (e.g. user has checked the box)? Shouldn't validators be all about
> semantics like correct value range or format?
>
> For a thing like "is Checkbox true/false" this is probably a close call,
> especially because the implementation will be minimalistic. But from a
> puristic view I think it would be false to put it in a validator at all.
>
> Any thoughts on this from you?
>
> bye, Michael
>
> simon.kitching@chello.at schrieb:
>
>
> > 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ß <mh...@ordix.de> 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?
> >>>
> >>
> >
> >
> >
>
>
> --
>
>
> Michael Heß
> Senior Consultant
>
> ORDIX AG
> Westernmauer 12-16
> 33098 Paderborn
>
> http://www.ordix.de
> Tel.: 05251 / 10 63 0
> Fax : 05251 / 10 63 99
> Mob : 0163 /673 49 43
>
> ORDIX AG - Aktiengesellschaft für Softwareentwicklung, Schulung, Beratung
> und Systemintegration
> Vorsitzender des Aufsichtsrates: Prof. Dr. Hermann Johannes
> Vorstand: Wolfgang Kögler (Vorsitzender), Benedikt Georgi, Christoph
> Lafeld, Axel Röber
> Firmensitz: Westernmauer 12 - 16, 33098 Paderborn, Tel: 05251 / 1063-0,
> Fax: 0180 / 1 67 34 90
> Amtsgericht Paderborn, HRB 2941, Ust-IdNr.DE 126333767, Steuernummer:
> 339/5866/0142
>
>
>
Re: Checkbox and required=true
Posted by Michael Heß <mh...@ordix.de>.
Thanks for you input.
I gave this some more thought on my way home last night. When I think
about it - is a validator the right place to do this kind of "business
logic" (e.g. user has checked the box)? Shouldn't validators be all about
semantics like correct value range or format?
For a thing like "is Checkbox true/false" this is probably a close call,
especially because the implementation will be minimalistic. But from a
puristic view I think it would be false to put it in a validator at all.
Any thoughts on this from you?
bye, Michael
simon.kitching@chello.at schrieb:
> 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ß <mh...@ordix.de> 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?
>>>
>>
>
>
>
--
Michael Heß
Senior Consultant
ORDIX AG
Westernmauer 12-16
33098 Paderborn
http://www.ordix.de
Tel.: 05251 / 10 63 0
Fax : 05251 / 10 63 99
Mob : 0163 /673 49 43
ORDIX AG - Aktiengesellschaft für Softwareentwicklung, Schulung, Beratung
und Systemintegration
Vorsitzender des Aufsichtsrates: Prof. Dr. Hermann Johannes
Vorstand: Wolfgang Kögler (Vorsitzender), Benedikt Georgi, Christoph
Lafeld, Axel Röber
Firmensitz: Westernmauer 12 - 16, 33098 Paderborn, Tel: 05251 / 1063-0,
Fax: 0180 / 1 67 34 90
Amtsgericht Paderborn, HRB 2941, Ust-IdNr.DE 126333767, Steuernummer:
339/5866/0142
Re: Checkbox and required=true
Posted by "simon.kitching@chello.at" <si...@chello.at>.
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ß <mh...@ordix.de> 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?
>>
>
Re: Checkbox and required=true
Posted by Martin Marinschek <ma...@gmail.com>.
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ß <mh...@ordix.de> 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?
>
> Thanks and bye,
> Michael
>
>
> --
> Michael Heß
> Senior Consultant
>
> ORDIX AG
> Westernmauer 12-16
> 33098 Paderborn
>
> http://www.ordix.de
> Tel.: 05251 / 10 63 0
> Fax : 05251 / 10 63 99
> Mob : 0163 /673 49 43
>
> ORDIX AG - Aktiengesellschaft für Softwareentwicklung, Schulung, Beratung
> und Systemintegration
> Vorsitzender des Aufsichtsrates: Prof. Dr. Hermann Johannes
> Vorstand: Wolfgang Kögler (Vorsitzender), Benedikt Georgi, Christoph
> Lafeld, Axel Röber
> Firmensitz: Westernmauer 12 - 16, 33098 Paderborn, Tel: 05251 / 1063-0,
> Fax: 0180 / 1 67 34 90
> Amtsgericht Paderborn, HRB 2941, Ust-IdNr.DE 126333767, Steuernummer:
> 339/5866/0142
>
>
>
--
http://www.irian.at
Your JSF powerhouse -
JSF Consulting, Development and
Courses in English and German
Professional Support for Apache MyFaces