#12363: Add add() method to ErrorDict class
-----------------------+----------------------------------------------------
 Reporter:  miracle2k  |       Owner:  nobody    
   Status:  new        |   Milestone:            
Component:  Forms      |     Version:  1.1       
 Keywords:             |       Stage:  Unreviewed
Has_patch:  0          |  
-----------------------+----------------------------------------------------
 When doing form validation, in some cases you want to add an error message
 to a field other than the one you are currently validating, or add an
 error message to a field while inside the form's global clean() method.
 Usually this is because in order to validate a given field you need access
 to the data of another field that is not yet cleaned.

 (For example, I have a form with two fields, "page" and "url", only one
 being required).

 Currently, doing this requires accessing the form._errors dict and
 injecting the error message. This gets cumbersome when you want to "do the
 right thing" and not override existing error messages. I.e. you have to do
 something like:

 {{{
 if does_not_validate():
     self._errors.setdefault('the_field', ErrorList())
     self._errors['the_field'] += ['Fix this value']
 }}}

 I feel like it would be a nice addition of ErrorDict had something like
 add() or set() methods:

 {{{
 if does_not_validate():
     self.errors.add('the_field', 'Fix this value')
 }}}

 If you want to override all existing errors, you could do:

 {{{
 if does_not_validate():
     self.errors.set('the_field', 'Fix this value')
 }}}

 Possibly an alternative approach would be to allow raising a
 ValidationError with a dict:

 {{{
 if does_not_validate:
     raise ValidationError({'the_field': 'Fix this value'})
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/12363>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--

You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.


Reply via email to