Re: booleanfield and required = true
On Thu, 2007-11-15 at 06:47 -0800, Milan Andric wrote: > > > On Nov 9, 12:34 am, Malcolm Tredinnick <[EMAIL PROTECTED]> > wrote: > > On Fri, 2007-11-09 at 06:26 +, Milan Andric wrote: > > > > > On Nov 9, 12:02 am, Malcolm Tredinnick <[EMAIL PROTECTED]> > > > wrote: > > > > > Shouldn't the form give errors and not validate? > > > > > > Nope. It's a bit tricky, though. The problem is that HTML forms do not > > > > send *anything* for a checkbox that you don't check. So missing data for > > > > a checkbox field means it wasn't checked and is, therefore, False. > > > > > Seems if I have (pseudocode) > > > > > Form: > > >f = someField(required=True) > > > > > When I pass no data into that form it should not validate? Just > > > doesn't seem right? > > > > In general, that will be true. However checkboxes are special: no data > > just means the value wasn't checked in the form, it doesn't mean that > > form field wasn't processed. It's an HTML oddity that Django has to > > accomodate. > > > > However, now that I'm thinking about this, there might be a bug here. > > Having required=True should mean you're required to check the box. There > > was a huge debate about this in a ticket, but the general acceptance > > amongst the maintainers and frequent contributors who commented was that > > that was the correct behaviour. > > > > I was responsible for checking in the change that makes no data = False > > for checkboxes (only), but I think I might have broken something in the > > process. I'll have to look a bit deeper into this at some point. > > > > Malcolm > > Ok, so I have a forms.BooleanField(required=True) ... that is pretty > much meaningless because when an html form is submitted the checkbox > is only present in the POST if it is checked. As I explained, boolean fields are special: required=True should be meaning "if it isn't checked it's an error". I also pointed out that the current behaviour is probably buggy. That hasn't changed. I'm not going to drop everything I'm doing to fix that right now, but I'll get to it eventually. Malcolm -- Experience is something you don't get until just after you need it. http://www.pointy-stick.com/blog/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: booleanfield and required = true
I haven't quite figured this out myself but here's what I do. By default all form fields have required=True. So, specifying required=True is not necessary. If the default value is True, pre- check the checkbox with initial={'checkme': True}. If the default value is False, specify required=False. This has the effect, as you noted, that the field is dropped from the posted data if the checkbox is unchecked. But, it also has the effect of allowing the form to validate the data correctly. Since it is not required, it doesnt matter to the form if it is missing from the data to be validated. Furthermore, when you retrieve the clean_data, the field will come out as False which is what you want. Hope this helps! Ken --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: booleanfield and required = true
On Nov 9, 12:34 am, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > On Fri, 2007-11-09 at 06:26 +, Milan Andric wrote: > > > On Nov 9, 12:02 am, Malcolm Tredinnick <[EMAIL PROTECTED]> > > wrote: > > > > Shouldn't the form give errors and not validate? > > > > Nope. It's a bit tricky, though. The problem is that HTML forms do not > > > send *anything* for a checkbox that you don't check. So missing data for > > > a checkbox field means it wasn't checked and is, therefore, False. > > > Seems if I have (pseudocode) > > > Form: > >f = someField(required=True) > > > When I pass no data into that form it should not validate? Just > > doesn't seem right? > > In general, that will be true. However checkboxes are special: no data > just means the value wasn't checked in the form, it doesn't mean that > form field wasn't processed. It's an HTML oddity that Django has to > accomodate. > > However, now that I'm thinking about this, there might be a bug here. > Having required=True should mean you're required to check the box. There > was a huge debate about this in a ticket, but the general acceptance > amongst the maintainers and frequent contributors who commented was that > that was the correct behaviour. > > I was responsible for checking in the change that makes no data = False > for checkboxes (only), but I think I might have broken something in the > process. I'll have to look a bit deeper into this at some point. > > Malcolm Ok, so I have a forms.BooleanField(required=True) ... that is pretty much meaningless because when an html form is submitted the checkbox is only present in the POST if it is checked. So I handle it manually in the view by setting the dict value to False. This still doesn't do anything because now a value has been submitted so required=True conditional passes. None of this makes sense to me at least: In [35]: class testForm(forms.Form): : checkme=forms.BooleanField(required=True) : : In [36]: f=testForm({}) In [37]: f.is_valid() Out[37]: True In [38]: f=testForm({'checkme':False}) In [39]: f.is_valid() Out[39]: True In [40]: f=testForm({'checkme':''}) In [41]: f.is_valid() Out[41]: False -- Milan --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: booleanfield and required = true
On Fri, 2007-11-09 at 06:26 +, Milan Andric wrote: > > > On Nov 9, 12:02 am, Malcolm Tredinnick <[EMAIL PROTECTED]> > wrote: > > > Shouldn't the form give errors and not validate? > > > > Nope. It's a bit tricky, though. The problem is that HTML forms do not > > send *anything* for a checkbox that you don't check. So missing data for > > a checkbox field means it wasn't checked and is, therefore, False. > > > > Seems if I have (pseudocode) > > Form: >f = someField(required=True) > > When I pass no data into that form it should not validate? Just > doesn't seem right? In general, that will be true. However checkboxes are special: no data just means the value wasn't checked in the form, it doesn't mean that form field wasn't processed. It's an HTML oddity that Django has to accomodate. However, now that I'm thinking about this, there might be a bug here. Having required=True should mean you're required to check the box. There was a huge debate about this in a ticket, but the general acceptance amongst the maintainers and frequent contributors who commented was that that was the correct behaviour. I was responsible for checking in the change that makes no data = False for checkboxes (only), but I think I might have broken something in the process. I'll have to look a bit deeper into this at some point. Malcolm -- Why can't you be a non-conformist like everyone else? http://www.pointy-stick.com/blog/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: booleanfield and required = true
On Nov 9, 12:02 am, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > > Shouldn't the form give errors and not validate? > > Nope. It's a bit tricky, though. The problem is that HTML forms do not > send *anything* for a checkbox that you don't check. So missing data for > a checkbox field means it wasn't checked and is, therefore, False. > Seems if I have (pseudocode) Form: f = someField(required=True) When I pass no data into that form it should not validate? Just doesn't seem right? Just so no again if you don't feel like explaining. ;) I appreciate the answer nonetheless. -- Milan --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: booleanfield and required = true
On Fri, 2007-11-09 at 05:40 +, Milan Andric wrote: > I might be misunderstanding the documentation about BooleanField. > http://www.djangoproject.com/documentation/newforms/#booleanfield > > but it seems if i have : > > >>> class Foo(forms.Form): > bool = forms.BooleanField(required=True) > >>> f=Foo({}) > >>> f.is_valid() > True > >>>f.cleaned_data > {'b': False} > > Doesn't that conflict with what the docs say? > > Shouldn't the form give errors and not validate? Nope. It's a bit tricky, though. The problem is that HTML forms do not send *anything* for a checkbox that you don't check. So missing data for a checkbox field means it wasn't checked and is, therefore, False. We tried making it None for a while, but this was more confusing than not (you'd never get back False, for a start), so we switched to treating it as False. Malcolm -- I just got lost in thought. It was unfamiliar territory. http://www.pointy-stick.com/blog/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---