Latest release: 0.3

SeznamCaptcha

Content

Usage

extensions:
    captcha: Contributte\SeznamCaptcha\DI\SeznamCaptchaExtension
1
2

Configuration

By default is auto: on and method: http, you can disable it and bind addCaptcha to your forms by yourself.

captcha:
    auto: off # on | off
    method: xmlrpc # http | xmlrpc
1
2
3

Form

captcha

Just register an extension and keep auto argument as it is.

use Nette\Application\UI\Form;

protected function createComponentForm()
{
    $form = new Form();

    $form->addCaptcha('captcha')
        ->setRequired('Are you robot?');

    $form->addSubmit('send');

    $form->onSuccess[] = function (Form $form) {
        dump($form['captcha']);
    };

    return $form;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Rendering

Automatic

{control form}
1

Manual

It needs a CaptchaContainer consists of 2 inputs image and code.

<form n:name="form">
    {input captcha-image}
    {input captcha-code}
</form>
1
2
3
4

Example

use Minetro\SeznamCaptcha\Forms\CaptchaHash;
use Minetro\SeznamCaptcha\Forms\CaptchaImage;
use Minetro\SeznamCaptcha\Forms\CaptchaInput;
use Minetro\SeznamCaptcha\Provider\CaptchaValidator;
use Minetro\SeznamCaptcha\Provider\ProviderFactory;
use Nette\Application\UI\Form;

/** @var ProviderFactory @inject */
public $providerFactory;

protected function createComponentForm()
{
    $form = new Form();

    $provider = $this->providerFactory->create();
    $form['image'] = new CaptchaImage('Captcha', $provider);
    $form['hash'] = new CaptchaHash($provider);
    $form['code'] = new CaptchaInput('Code');

    $form->addSubmit('send');

    $form->onValidate[] = function (Form $form) use ($provider) {
        $validator = new CaptchaValidator($provider);

        $hash = $form['hash']->getHttpHash();
        $code = $form['code']->getHttpCode();

        if ($validator->validate($code, $hash) !== TRUE) {
            $form->addError('Are you robot?');
        }
    };

    $form->onSuccess[] = function (Form $form) {
        dump($form);
    };

    return $form;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

For better usability add this functionality to your BaseForms, BaseFormFactory or something like this.

You can also create a trait for it.