Skip to content

Erwane/cakephp-hcaptcha

Repository files navigation

HCaptcha plugin for CakePHP

Software License codecov Build Status Packagist Downloads Packagist Version

Version map

branch CakePHP core PHP min
1.x ^4.0 PHP 7.2
2.x ^5.0 PHP 8.1

Installation

composer require erwane/cakephp-hcaptcha

Load plugin in your src/Application::bootstrap()

    public function bootstrap(): void
    {
        $this->addPlugin('HCaptcha');
    }

Configuration

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

Usage

In your templates

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,
]) ?>

Validation

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']);
}

About

HCaptcha module for CakePHP 4.x

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages