Raphael
Raphael

Reputation: 8192

Django - Change the default error messages for a form

I'm trying to create a simple ajax form with a validation summary so I can't have the error message 'This field is required' show up n times. What I need to do is replace these messege with one message: 'Some fields are required.'.

The documentation is really scarse with form validation and I can't find any examples on how to achieve this. I have tried to iterate through the errors attribute of self on the clean method but apparently I can't set that attribute.

Any ideias?

Upvotes: 1

Views: 877

Answers (2)

If you don't need the errors, but still need validation, why not simply return that ajax response if not form.is_valid()?

if not form.is_valid():
    self.ctx['errors'] = 'Some fields are required.'
    self.ctx['success'] = False
# or some such

Upvotes: 2

JamesO
JamesO

Reputation: 25946

You can do this with the template. The default template when using {{form}} outputs the the individual field messages so create a template which doesn't e.g remove {{ field.errors }}

Upvotes: 1

Related Questions