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. urlquote() and urlencode() in one method by guettli 4 years, 6 months ago
  2. lazy url reverse() by guettli 5 years, 5 months ago
  3. Alter Column Lengths of Contrib Apps by guettli 5 years, 1 month ago
  4. newforms: Add field-specific error in form.clean() by miracle2k 5 years, 10 months ago
  5. AgreementField by chrisrbennett 4 years, 11 months ago

Comments

(Forgotten your password?)