#29205: MultiValueField ignores a required value of a sub field
--------------------------------+---------------------------------------
Reporter: Takayuki Hirai | Owner: David Smith
Type: Bug | Status: assigned
Component: Forms | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+---------------------------------------
Comment (by David Smith):
I've done some investigation into this. Here are some notes which explain
the behaviour outlined in the ticket.
**is_valid()**
The
[https://github.com/django/django/blob/49275c548887769cd70bbd85a3b125491f0c4062/django/forms/fields.py#L1006
clean] method of MultiValueField is driving the validation behaviour
explained in the original ticket.
The example in the ticket shows `required=False` being set on the
`MultiValueField`. When the fields are all blank (or two empty strings)
the logic follows this if statement and therefore doesn't raise a
validation error and `return self.compress([])`
{{{
if not value or isinstance(value, (list, tuple)):
if not value or not [v for v in value if v not in
self.empty_values]:
if self.required:
raise ValidationError(self.error_messages['required'],
code='required')
else:
return self.compress([])
}}}
In the case where one of the fields has some data, it skips this `if`
statement. The next section of clean loops over `fields` and therefore
raises an error as one of them contains `required=True`
**Required attribute**
The notes above also explain that is the field is `required=True` (default
setting) then whilst is_valid() gives the expected output both of the
fields input html tags both become `required`. This is due to this piece
of code in
[https://github.com/django/django/blob/b9cf764be62e77b4777b3a75ec256f6209a57671/django/forms/boundfield.py#L221
boundfield.py]. This code looks at the field required status rather than
subwidgets. Therefore all of the inputs for subwidgets are treated the
same, irrespective of each subwidgets `required` attribute.
I therefore think that the two areas above are where we should be looking
at for a potential fix. However, I'm not quite sure what is intended to
happen where some widgets are required and others are not. Some questions,
appreciate thoughts.
- Should inputs have required html attributes (all, some, none)
- How should the help / validation text work. (e.g. 'this field is
required')
--
Ticket URL: <https://code.djangoproject.com/ticket/29205#comment:12>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/064.d9b537034ed3af715338049c9f6aeffb%40djangoproject.com.