Skip to content
This is the documentation for pieceofcake2/captcha, a CakePHP 2.x plugin for pieceofcake2/cakephp. About this fork.

Rendering the Widget (CaptchaHelper) ​

Add the helper to your controller (or AppController). Settings are merged over the Captcha configuration:

php
public $helpers = ['Captcha.Captcha' => ['theme' => 'light']];

widget(array $options = []) ​

Renders the widget. Call it inside a form:

php
echo $this->Form->create('Contact');
echo $this->Form->input('message');
echo $this->Captcha->widget();
echo $this->Form->end('Send');
html
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async="async" defer="defer"></script>
<div class="cf-turnstile" data-sitekey="1x00000000000000000000AA" data-theme="light"></div>

The provider script adds a hidden field with the token to the form (cf-turnstile-response, g-recaptcha-response or h-captcha-response).

Options:

  • Widget options of the provider, e.g. ['theme' => 'dark', 'size' => 'compact', 'action' => 'login']. They override the configuration.
  • script: The provider script is output before the first widget. Pass false when you output it yourself with script(), or an array of script() options.
  • nonce: CSP nonce for the script tags.
  • Any other key is an HTML attribute (id, class is appended, data-*, ...).

When FormHelper is loaded, the widget unlocks the token fields (FormHelper::unlockField()), so forms protected by SecurityComponent are not black-holed.

script(array $options = []) ​

Returns the provider <script> tag, once per page. Use it when you want the script in the <head>:

php
$this->Captcha->script(['block' => 'script']);
OptionDefaultDescription
blockfalseAppend the tag to this view block instead of returning it.
async, defertrueAdd the async / defer attributes.
noncenonce settingCSP nonce.
renderexplicit to render the widgets yourself with the provider's JavaScript API.
onloadName of a global function called when the script is loaded.
languagelanguage settinghl parameter (reCAPTCHA, hCaptcha).
queryAdditional query parameters, e.g. ['recaptchacompat' => 'off'] for hCaptcha.
oncetrueOutput the tag only once.

provider() ​

Returns the configured CaptchaProviderInterface instance.

See LICENSE for copyright and license information. Not affiliated with the Cake Software Foundation.