Yeah I'm definitely on the "document add_error first, ValidationError(dict) as a more powerful alternative". The second option is necessary to do clever things directly from Model.clean()
Marc On 20 August 2013 07:07, Anssi Kääriäinen <[email protected]> wrote: > 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 <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<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+unsubscribe@**googlegroups.com<django-developers%[email protected]> > . > To post to this group, send email to > django-developers@**googlegroups.com<[email protected]> > . > Visit this group at > http://groups.google.com/**group/django-developers<http://groups.google.com/group/django-developers> > . > For more options, visit > https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> > . > -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-developers. For more options, visit https://groups.google.com/groups/opt_out.
