George Notaras avatar 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

Comments (0)

Files changed (1)

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
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.