From 7ac8380799eedb374621317b62ccf026d86ea245 Mon Sep 17 00:00:00 2001 From: Loic Bistuer Date: Sun, 23 Mar 2014 01:08:04 +0700 Subject: Fixed #22318 -- Added Form.has_error() to easily check if a given error has happened. --- django/forms/forms.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'django/forms') diff --git a/django/forms/forms.py b/django/forms/forms.py index da30a6220a..97a344e9fc 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -334,6 +334,15 @@ class BaseForm(object): if field in self.cleaned_data: del self.cleaned_data[field] + def has_error(self, field, code=None): + if code is None: + return field in self.errors + if field in self.errors: + for error in self.errors.as_data()[field]: + if error.code == code: + return True + return False + def full_clean(self): """ Cleans all of self.data and populates self._errors and -- cgit v1.3