branch | CakePHP core | PHP min |
---|---|---|
1.x | ^4.0 | PHP 7.2 |
2.x | ^5.0 | PHP 8.1 |
composer require erwane/cakephp-hcaptcha
Load plugin in your src/Application::bootstrap()
public function bootstrap(): void
{
$this->addPlugin('HCaptcha');
}
In your config/app.php
, insert this default values:
// If you use .env file:
'HCaptcha' => [
'key' => env('HCAPTCHA_KEY'),
'secret' => env('HCAPTCHA_SECRET'),
],
// If you use config/app_local.php
'HCaptcha' => [
'key' => null,
'secret' => null,
],
HCaptcha key and secret can be found in your HCaptcha dashboard
Add the captcha to your form
<?= $this->Form->control('h-captcha-response', ['type' => 'hcaptcha']) ?>
You can pass options to hCaptcha.
<?= $this->Form->control('h-captcha-response', [
'type' => 'hcaptcha',
'lang' => 'fr_FR',
'onload' => 'myFunction',
'render' => 'explicit',
'recaptchacompat' => false,
]) ?>
In your Model
or Form
validation, add hCaptcha validation provider and define your rule.
use Cake\Validation\Validator;
public function validationDefault(Validator $validator): Validator
{
$validator->setProvider('HCaptcha', '\HCaptcha\Validation');
return parent::validationDefault($validator)
->add('h-captcha-response', 'hcaptcha', ['provider' => 'HCaptcha', 'rule' => 'hcaptcha']);
}