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.