Info
I adapt my model form to my html form design, It can submit data successfully, It has placeholder which is I wrote , It gives the error when you forget to input data required fields.
However, I couldn’t change default message. It is always, “This value is required.”
My model
class MyModel(models.Model):
a_field = models.CharField(max_length=254,)
b_field = models.EmailField(max_length =254)
...other fields...
I’m sharing you what I tried to change error message on the forms.
#forms.py
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('a_field', 'b_field', 'c_field', ....other fields)
widgets = {
'a_field':forms.TextInput(attrs={'placeholder':'Placeholder a'}),
'b_field':forms.TextInput(attrs={'placeholder':'Placeholder b'}),
.... others...
}
First, I tried this.
error_messages = { 'a_field': { 'required': _("Custom required message"), }, }
In the def init… I tried these things,
def __init__(self, *args, **kwargs):
super(PersonForm, self).__init__(*args, **kwargs)
I added this lines,
self.fields['a_field'].error_messages.update({ 'required': 'Required Message', })
Doesn’t change the error message…
self.fields['b_field'].error_messages = {'required': 'custom required message'}
I tried to add this too for all fields but nothing changes.
for field in self.fields.values(): field.error_messages = {'required':'The field {fieldname} is required'.format( fieldname=field.label)}
My template currently:
…
<form method="post" novalidate class="form-control contact-form__inner" action="{% url 'demo'%}">
{% csrf_token %}
<div class="contact-form__inner-left">
<div class="form-item form-item--border">
{{ form.field_a }}
</div>
<div class="form-item form-item--border">
{{ form.field_b }}
</div>
…
My template before I manipulate the form with my modelform,
This required messages were visible.
<form action="" class="form-control contact-form__inner">
<div class="contact-form__inner-left">
<div class="form-item form-item--border">
<input type="text" placeholder="Placeholder a" data-parsley-required-message="Required message." required>
</div>
<div class="form-item form-item--border">
<input type="text" placeholder="Placeholder b" data-parsley-required-message="Required message." required>
</div>
…
What am I missing ? Why I can’t change the error messages ?
Thanks in advance…