简体   繁体   中英

django: Required Field Not Having The “required” Attribute in HTML

I am new to django and I think it is fantastic so far. I faced this weird issue today: I have this FormModel:

class RegisterForm2(UserCreationForm):
    mobile2 = forms.CharField(
        label=_('Mobile Confirmation'),
        max_length=50,
        required=True,
        widget = forms.TextInput(attrs = {'class':'nocopy'})
    )

    class Meta:
        model = User
        fields = ['username', 'mobile', 'mobile2', 'guardian_mobile']
        labels = {
            'username': _('Government ID'),
        }
        widgets = {
            # workaround since __init__ setting to required doesnt work
            'mobile': forms.TextInput(attrs = {'required':''}),
        }

    def __init__(self, *args, **kwargs):
        super(RegisterForm2, self).__init__(*args, **kwargs)

        self.fields['mobile'].required = True

Now mobile field has blank=True in the model but i want it to be required in the form only and so I did that in the __init__ . Now it is making the field required but it is not adding required="" to the text input. Now if i have overridden the field (and added the required=True ) like I did mobile2 field I wouldn't have this issue. This is a an issue because if i want the attribute required to show on fields i have to override the fields in the form to add the required=True which will go against the DRY principle. Am I missing something or is this a bug of some sort?

Edit: I failed to mention that I am using floppy forms. It could be related to that. I have to investigate this issue further.

Note this answer assumes you are using regular Django forms, I wrote it before you mentioned that you were using floppy forms.

Django does not currently add the required HTML 5 attribute when it renders form fields. Overriding the widget for the mobile field sounds looks OK to me. You are changing the widget, not duplicating the field, so I don't think you need to worry about it being DRY.

Note that setting required in the __init__ is having an effect - if you submit the form with an empty value for mobile , then you will get a validation error. Your mobile2 field will not have required in the rendered HTML unless you add it to its widget attrs.

Django will add the required attribute to form fields in Django 1.10, see ticket #22383 .

This is actuially possible in your __init__ . You just have to add the required attribute to the widget attributes:

def __init__(self, *args, **kwargs): super(RegisterForm2, self).__init__(*args, **kwargs) self.fields['mobile'].required = True self.fields['mobile'].widget.attrs['required'] = 'required'

but i want it to be required in the form only

'mobile': forms.TextInput(attrs = {'required':''}),

You can do this in views.py also. That's how I prefer to do it.

form.fields['mobile'].widget.attrs['required'] = True 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM