I have a form widget (for the bio
field) which is not being required by the form (no validation, no HTML attribute set to required
) despite me setting it explicitly in the form constructor (as suggested in this SO question). When I say it is not required, I mean I can submit the form with the field empty (which I am blocked from doing for other fields):
users/forms.py
:
from wagtail.admin.rich_text import get_rich_text_editor_widget
class UpdateProfileForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(UpdateProfileForm, self).__init__(*args, **kwargs)
self.fields['bio'].required = True
class Meta():
model = _user_profiles.__model__
fields = (
'email', 'first_name', 'last_name', 'organisation',
'bio', 'city', 'country', 'photo_file'
)
widgets = {
'bio': get_rich_text_editor_widget('submission')
}
If I set a breakpoint in users/views.py
I can see that all the way until the end of the stack, the form field is correctly set to 'required'=True
, just like the email
field which the form correctly requires, and unlike city
which it does not require:
users/views.py
:
> /root/.pyenv/versions/3.7.2/lib/python3.7/site-packages/django/views/generic/base.py(151)get()
150 context = self.get_context_data(**kwargs)
--> 151 return self.render_to_response(context)
152
ipdb> context['form'].fields['bio'].required
True
ipdb> context['form'].fields['email'].required
True
ipdb> context['form'].fields['city'].required
False
Any ideas why the field is not required?