Opened 3 years ago
Closed 3 years ago
#34400 closed Bug (invalid)
ModelFormSet not adding 'required' to fields
| Reported by: | DuraNathOG | Owned by: | nobody |
|---|---|---|---|
| Component: | Forms | Version: | 4.0 |
| Severity: | Normal | Keywords: | form modelform modelformset |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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.
Note:
See TracTickets
for help on using tickets.
The
requiredattribute of widgets is set when the widget is rendered, not when the form is instantiated.