1

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?

KindOfGuy
  • 3,081
  • 5
  • 31
  • 47
  • 1
    I'm having a bit of trouble following your question (you haven't explicitly stated which field is showing as non-required, or how exactly that behaviour is manifesting itself on the form) but it's possible that you've run into this bug in Wagtail's RichTextField: https://github.com/wagtail/wagtail/issues/4549 – gasman May 03 '19 at 18:58
  • Ah, that does indeed seem to be it, thank you! The form won't save and the field is required if I focus that field at some point. I see the proposed solution was never actioned. Any tips from someone in my position? – KindOfGuy May 03 '19 at 20:28
  • Perhaps I can auto-focus on that input box with JS on page load. – KindOfGuy May 07 '19 at 11:07

0 Answers0