I have a ModelForm and I want to customize some of the error messages for required fields. Some of the customized error messages work, but some don't. Here is my code:
error_messages = {
'photo': {
'required': _("A profile picture is required."),
},
'height': {
'required': _("Your height is required."),
},
'diet': {
'required': _("Your diet is required."), # ~~~~ TODO: not working.
},
'smoking_status': {
'required': _("Your smoking status is required."), # ~~~~ TODO: not working.
},
'relationship_status': {
'required': _("Your relationship status is required."), # ~~~~ TODO: not working.
},
**{to_attribute(name='profile_description', language_code=language_code): {
'required': _("Please write a few words about yourself."),
} for language_code, language_name in django_settings.LANGUAGES},
**{to_attribute(name='city', language_code=language_code): {
'required': _("Please write where you live."), # ~~~~ TODO: not working.
} for language_code, language_name in django_settings.LANGUAGES},
**{to_attribute(name='children', language_code=language_code): {
'required': _("Do you have children? How many?"),
} for language_code, language_name in django_settings.LANGUAGES},
**{to_attribute(name='more_children', language_code=language_code): {
'required': _("Do you want (more) children?"),
} for language_code, language_name in django_settings.LANGUAGES},
**{to_attribute(name='match_description', language_code=language_code): {
'required': _("Who is your ideal partner?"),
} for language_code, language_name in django_settings.LANGUAGES},
'gender_to_match': {
'required': _("Gender to match is required."), # ~~~~ TODO: not working.
},
'min_age_to_match': {
'required': _("Minimal age to match is required."),
},
'max_age_to_match': {
'required': _("Maximal age to match is required."),
},
'diet_match': {
'required': _("Diet match is required."),
},
'smoking_status_match': {
'required': _("Smoking status match is required."),
},
'relationship_status_match': {
'required': _("Relationship status match is required."),
},
}
https://github.com/speedy-net/speedy-net/blob/staging/speedy/match/accounts/forms.py#L100-L149
I marked the custom error messages which are not working with # ~~~~ TODO: not working.
. The others are working.
Any suggestions?
__init__
method of your form. Theerror_messages
Meta
option only works for fields that are not overridden (it's just used by theModelForm
to create the fields automatically from theModel
). – Charitycharivari