From bc884cac301ee68211e07da90b05ca75f5b9ad4a Mon Sep 17 00:00:00 2001 From: Robert Kajic Date: Mon, 8 Oct 2012 14:16:43 +0200 Subject: [PATCH] Made it so that captcha errors are associated with the captcha field instead of the whole form --- math_captcha/forms.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/math_captcha/forms.py b/math_captcha/forms.py index 022af0e..5b70be4 100644 --- a/math_captcha/forms.py +++ b/math_captcha/forms.py @@ -26,16 +26,18 @@ def math_clean(form): assert len(test_secret) == 40 and question except (TypeError, AssertionError): # problem decoding, junky data - raise forms.ValidationError('Invalid token') + form._errors['math_captcha_field'] = form.error_class(["Invalid token"]) + del form.cleaned_data['math_captcha_field'] except KeyError: return if encode(question) != form.cleaned_data['math_captcha_question']: # security problem, hack attempt - raise forms.ValidationError('Invalid token') + form._errors['math_captcha_field'] = form.error_class(["Invalid token"]) + del form.cleaned_data['math_captcha_field'] if eval(question) != value: - raise forms.ValidationError('Wrong answer, try again') - + form._errors['math_captcha_field'] = form.error_class(["Wrong answer, try again"]) + del form.cleaned_data['math_captcha_field'] class MathCaptchaModelForm(forms.ModelForm): """