https://github.com/cyber-duck/silverstripe-recaptcha
Standard reCAPTCHA and invisible reCAPTCHA form fields for SilverStripe
https://github.com/cyber-duck/silverstripe-recaptcha
google php recaptcha silverstripe silverstripe-4
Last synced: 4 months ago
JSON representation
Standard reCAPTCHA and invisible reCAPTCHA form fields for SilverStripe
- Host: GitHub
- URL: https://github.com/cyber-duck/silverstripe-recaptcha
- Owner: Cyber-Duck
- License: mit
- Created: 2018-10-19T13:09:00.000Z (over 7 years ago)
- Default Branch: develop
- Last Pushed: 2023-09-08T10:11:40.000Z (almost 3 years ago)
- Last Synced: 2024-09-18T18:47:32.659Z (almost 2 years ago)
- Topics: google, php, recaptcha, silverstripe, silverstripe-4
- Language: PHP
- Size: 20.5 KB
- Stars: 4
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Silverstripe-Recaptcha
Standard reCAPTCHA and invisible reCAPTCHA form fields for SilverStripe
[](https://packagist.org/packages/cyber-duck/silverstripe-recaptcha)
[](https://packagist.org/packages/cyber-duck/silverstripe-recaptcha)
[](https://packagist.org/packages/cyber-duck/silverstripe-recaptcha)
[](https://packagist.org/packages/cyber-duck/silverstripe-recaptcha)
Author: [Andrew Mc Cormack](https://github.com/Andrew-Mc-Cormack)
__For SilverStripe 4.*__
## Installation
Add the following to your composer.json file and run /dev/build?flush=all
```json
{
"require": {
"cyber-duck/silverstripe-recaptcha": "4.0.*"
}
}
```
## Standard Recaptcha
Add 2 .env vars with your recaptcha keys
```
RECAPTCHA_SITE_KEY="0000000000"
RECAPTCHA_SECRET_KEY="0000000000"
```
Add the field to any form on your site just as you would any other field.
Pass in:
- the field name.
Also add the field to the required fields list.
```php
use CyberDuck\Recaptcha\Forms\RecaptchaField;
$fields = FieldList::create([
RecaptchaField::create('Recaptcha');
]);
$validator = RequiredFields::create([
'Recaptcha'
]);
```
## Invisible Recaptcha
Add 2 .env vars with your recaptcha keys
```
RECAPTCHA_INVISIBLE_SITE_KEY="0000000000"
RECAPTCHA_INVISIBLE_SECRET_KEY="0000000000"
```
Add the field to any form on your site just as you would any other field.
Pass in:
- the field name,
- the form ID (without #)
- the HTML element ID (without #) or selector (such as button) to render the reCAPTCHA widget.
Also add the field to the required fields list.
```php
use CyberDuck\Recaptcha\Forms\InvisibleRecaptchaField;
$fields = FieldList::create([
InvisibleRecaptchaField::create(
'Recaptcha',
'MemberLoginForm_LoginForm',
'MemberLoginForm_LoginForm_action_doLogin'
);
]);
$validator = RequiredFields::create([
'Recaptcha'
]);
```
## Working with jQuery and AJAX
If you would like to use the reCAPTCHA with AJAX you can copy the InvisibleRecaptcha.ss from the module templates folder into your theme templates folder
```javascript
var onRecaptchaFormSubmit = function(token) {
document.getElementById('{$FormID}').submit();
};
var onloadCallback = function() {
grecaptcha.render('{$Container}', {
'sitekey' : '{$SiteKey}',
'callback' : onRecaptchaFormSubmit
});
};
```
And just update the onRecaptchaFormSubmit function to use jQuery to select and submit the form
```javascript
var onRecaptchaFormSubmit = function(token) {
$('#{$FormID}').submit();
};
var onloadCallback = function() {
grecaptcha.render('{$Container}', {
'sitekey' : '{$SiteKey}',
'callback' : onRecaptchaFormSubmit
});
};
```
In your AJAX success function, if your form fails validation you can call onloadCallback() to re-render the reCAPTCHA
```javascript
onloadCallback()
```