[Django] #34400: ModelFormSet not adding 'required' to fields

2 views
Skip to first unread message

Django

unread,
Mar 10, 2023, 9:22:05 AM3/10/23
to django-...@googlegroups.com
#34400: ModelFormSet not adding 'required' to fields
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
DuraNathOG |
Type: Bug | Status: new
Component: Forms | Version: 4.0
Severity: Normal | Keywords: form modelform
Triage Stage: | modelformset
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
When working with ModelFormSet, the <input> tag is not getting the
'required' attribute by default.

I've fixed this in my project by adding the following to the form.


{{{
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
for key, value in self.fields.items():
if value.required:
self.fields[key].widget.attrs['required'] = 'required'
}}}

Without this, the form posts to the server and fails validation, when it
can/should be handled client side first. The fact 'if value.required' can
be tested here leads me to believe this is a bug and should be default
behaviour.

--
Ticket URL: <https://code.djangoproject.com/ticket/34400>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 10, 2023, 1:57:44 PM3/10/23
to django-...@googlegroups.com
#34400: ModelFormSet not adding 'required' to fields
-------------------------------------+-------------------------------------
Reporter: DuraNathOG | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 4.0
Severity: Normal | Resolution: invalid

Keywords: form modelform | Triage Stage:
modelformset | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

The `required` attribute of widgets is set when the widget is
[https://github.com/django/django/blob/4b1bfea2846f66f504265cec46ee1fe94ee9c98b/django/forms/widgets.py#L264
rendered], not when the form is instantiated.

--
Ticket URL: <https://code.djangoproject.com/ticket/34400#comment:1>

Reply all
Reply to author
Forward
0 new messages