I can't use `forms.fields.BooleanField` because when `required=True` is
set Yes *must* be selected for the field to validate.
This topic has come up in the past: <https://groups.google.com/d/msg
/django-developers/eN7FvAvTsew/kLQ4FdE3fWAJ>
I propose `forms.fields.NullBooleanField` be altered to handle required.
That is change validate to:
{{{
def validate(self, value):
if value is None and self.required:
raise ValidationError(....
}}}
I'll happily create a pull request to achieve this. One concern will be
backwards compatibility. But seeing as how `required=True` had no effect
on the field originally, I see no reason anyone would set it and expect
something useful other than what has been suggested above.
--
Ticket URL: <https://code.djangoproject.com/ticket/23580>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: jon.dufresne@… (added)
* needs_better_patch: => 0
* has_patch: 0 => 1
* needs_tests: => 0
* needs_docs: => 0
Comment:
Submitted pull required: <https://github.com/django/django/pull/3297>
--
Ticket URL: <https://code.djangoproject.com/ticket/23580#comment:1>
Comment (by claudep):
What would then be the difference between your proposal
(`forms.fields.NullBooleanField` with `required=True`) and
`forms.fields.BooleanField` with `required=False`?
--
Ticket URL: <https://code.djangoproject.com/ticket/23580#comment:2>
Comment (by timgraham):
I think the primary difference is the widget used. In the case of
`NullBooleanField`, you get a `<select>` where the user must pick `True`
or `False`, whereas `BooleanField` is a checkbox where no explicit
selection must be made.
The fix for #23130 may address the use case of this ticket.
--
Ticket URL: <https://code.djangoproject.com/ticket/23580#comment:3>
Comment (by jdufresne):
> What would then be the difference between your proposal
(forms.fields.NullBooleanField with required=True) and
forms.fields.BooleanField with required=False?
`forms.fields.BooleanField` does not allow for a "no answer". In my
application the user must *explicitly* select Yes or No. Assuming No when
no answer is provided is not an option.
Just for some clarification, I'm rendering this field as a yes/no radio
option:
{{{
This field is required.
Do you agree with foo?
[ ] Yes
[ ] No
}}}
With my proposal above, the user would need to explicitly select No for
the field to evaluate as `False`, if the field is required and and they do
not select No, they will be warned.
--
Ticket URL: <https://code.djangoproject.com/ticket/23580#comment:4>
Comment (by jdufresne):
> The fix for #23130 may address the use case of this ticket.
If I understand that ticket correctly (and perhaps I don't) that looks to
be dealing with the model layer and how it creates forms in the admin
site. Is this correct?
My use case has no connection to models nor the admin site. This is just a
straight form POST to a view with validating and processing that data.
--
Ticket URL: <https://code.djangoproject.com/ticket/23580#comment:5>
* status: new => closed
* resolution: => invalid
Comment:
I don't think excluding the validity of the `Unknown` choice when
`required=True` is a good idea at all.
I think that your use case is fulfilled by something like:
{{{
forms.ChoiceField(widget=forms.RadioSelect, choices=((1, 'Yes'), (0,
'No')), required=True)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23580#comment:6>
Comment (by jdufresne):
> think that your use case is fulfilled by something like:
>
> `forms.ChoiceField(widget=forms.RadioSelect, choices=((1, 'Yes'), (0,
'No')), required=True)`
One of the issues with this, it evaluates to the string `'1'` and `'0'`
(or `'True'` and `'False'`) and not the values: `True`, `False`, `None` as
the `NullBooleanField` does. I think that is the major downside here.
So in the end, a custom field and widget is required by the application
writer for something that seems very simple on the surface. That is, a
boolean form field that does not default to `False` and must be answered
either `True`/`False`.
Obviously one can always add this into their own project -- and maybe my
proposal is the incorrect approach -- but I still think it would be nice
to have something to accomplish this built-in.
--
Ticket URL: <https://code.djangoproject.com/ticket/23580#comment:7>
Comment (by claudep):
This might be a topic for a django-developers discussion, to see if you
can get some traction for your suggestion.
--
Ticket URL: <https://code.djangoproject.com/ticket/23580#comment:8>