Commits

George Notaras  committed 56c5b43

Fixed bug: When the recaptcha field is not required, do not try to validate the response field if it is empty

  • Participants
  • Parent commits 46613ef

Comments (0)

Files changed (1)

File src/recaptcha_works/fields.py

         value = super(RecaptchaField, self).clean(value)
         challenge, response, remote_ip = value
         
+        # Validation overrides
         if settings.RECAPTCHA_VALIDATION_OVERRIDE:
             return value
+        if not response and not self.required:
+            return value
+        
         if not challenge:
             raise forms.ValidationError(self.error_messages['challenge-error'])
         if not response:
-            if self.required:
-                raise forms.ValidationError(self.error_messages['required'])
+            raise forms.ValidationError(self.error_messages['required'])
         if not remote_ip:
             raise forms.ValidationError(self.error_messages['no-remote-ip'])
         try:
             value = validate_recaptcha(remote_ip, challenge, response,
                                        self.private_key, self.use_ssl)
         except RecaptchaError, e:
-            # If the ``required`` attribute has been set to False, do not
-            # raise any errors, but pass validation instead. This is meant
-            # to be used as a validation override for a single reCaptcha
-            # protected form, instead of using the global override switch
-            # RECAPTCHA_VALIDATION_OVERRIDE
-            if self.required:
-                if e.code == 'incorrect-captcha-sol':
-                    raise forms.ValidationError(self.error_messages['invalid'])
-                raise forms.ValidationError(self.error_messages['unknown-error'] %
-                                            {'code': e.code})
+            if e.code == 'incorrect-captcha-sol':
+                raise forms.ValidationError(self.error_messages['invalid'])
+            raise forms.ValidationError(self.error_messages['unknown-error'] %
+                                        {'code': e.code})
         return value