On 08/20/2013 08:26 AM, Loic Bistuer wrote:
An alternate would be to implement a method that does this.

def field_error(self, name, error):
     self._errors = self._errors or ErrorDict()
     self._errors.setdefault(name, ErrorList())
     self._errors[name].append(error)
I also have a pending PR for that: https://code.djangoproject.com/ticket/5335.

So you could do `self._errors[name].append(error)` from within the form and 
`self.errors[name].append(error)` from outside.

Having to raise a ValidationError would prevent you from creating multiple 
field errors from within `clean`.

Not necessarily, the `ValidationError` constructor accepts a variety of 
scenarios:

def clean(self):
     errors = {}
     if condition1:
         errors['field1'] = ValidationError('message1', code='code1')
     if condition2:
         errors['field2'] = ValidationError('message2', code='code2')
     raise ValidationError(errors)

Note that in the example above,  `ValidationError('message1', code='code1')` 
can also be a simple string, but if https://github.com/django/django/pull/1483 
(yet another PR) goes in, passing an error code will become quite useful.

I really like the Form.add_error(field, error_message) approach. I have used it as custom addition in some of my projects, and it just feels correct.

 - Anssi

--
You received this message because you are subscribed to the Google Groups "Django 
developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to