Add ValidationError to a field instead of __all__ during Form.clean()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Related http://code.djangoproject.com/ticket/5335
def errors_append(form, field_name, message):
    u'''
    Add an ValidationError to a field (instead of __all__) during Form.clean():

    class MyForm(forms.Form):
        def clean(self):
            value_a=self.cleaned_data['value_a']
            value_b=self.cleaned_data['value_b']
            if value_a==... and value_b==...:
                formutils.errors_append(self, 'value_a', u'Value A must be ... if value B is ...')
            return self.cleaned_data
    '''
    assert form.fields.has_key(field_name), field_name
    error_list=form.errors.get(field_name)
    if error_list is None:
        error_list=forms.util.ErrorList()
        form.errors[field_name]=error_list
    error_list.append(message)
    

More like this

  1. Dynamically add css-classes to formfields by Lacour 2 years, 9 months ago
  2. DateTimeField with microseconds by tobias 5 years, 1 month ago
  3. CustomChoiceField, Selectable label field version of ModelChoiceField by mauro 6 years, 1 month ago
  4. Custom model field for Frame or Box by binishkaspar 3 years, 1 month ago
  5. Smart {% if %} template tag by SmileyChris 5 years, 1 month ago

Comments

(Forgotten your password?)