long399/yii2-math-captcha

yii2 MathCaptchaAction

0.1.2 2022-05-05 08:38 UTC

This package is auto-updated.

Last update: 2024-04-05 12:56:50 UTC


README

Total Downloads License

MathCaptchaAction for Yii Framework 2.0

Screenshot

Installation

The preferred way to install this extension is through composer. Either run

 composer require --prefer-dist long399/yii2-math-captcha

or add

"long399/yii2-math-captcha": "~0.1"

to the require section of your composer.json file.

Available operations

  1. Addition.
  2. Subtraction.
  3. Multiplication.

Default addition or subtraction operation will be used randomly.

Usage

controller:

public function actions()
{
    return [
        ...
        'captcha' => [
            'class' => \long399\captcha\MathCaptchaAction::class,
            'fixedVerifyCode' => YII_ENV_TEST ? '399' : null,
            'minLength' => 0,
            'maxLength' => 1000,
        ],
        ...
    ];
}

model:

class MyModel extends \yii\db\ActiveRecord
{
    public $captcha;
    ...
    public function rules()
    {
        return [
            ...
            ['captcha', 'captcha', 'captchaAction' => '/site/captcha'],
            ...
        ];
    }
    ...
}

view:

...
echo $form->field($model, 'captcha')->widget(\yii\captcha\Captcha::class, [
    'captchaAction' => "/site/captcha",
    'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
]);
...

If you want to use only expressions with a subtraction operation e.g., then you need to set the operations property accordingly in the description of the action in the controller:

public function actions()
{
    return [
        ...
        'captcha' => [
            'class' => \app\components\actions\MathCaptchaAction::class,
            'fixedVerifyCode' => YII_ENV_TEST ? '399' : null,
            'minLength' => 0,
            'maxLength' => 1000,
            'operations' => ['-'],
        ],
        ...
    ];
}

Expressions with multiplication

If you want to use also expressions with a multiplication operation, then you need to set the operations property accordingly in the description of the action in the controller:

public function actions()
{
    return [
        ...
        'captcha' => [
            'class' => \app\components\actions\MathCaptchaAction::class,
            'fixedVerifyCode' => YII_ENV_TEST ? '399' : null,
            'minLength' => 0,
            'maxLength' => 1000,
            'operations' => ['+', '-', '*'],
        ],
        ...
    ];
}

Three kinds of expressions can be generated:

  1. Expression with multiplication.
    Screenshot
  2. Expression with multiplication and addition.
    Screenshot
  3. Expression with multiplication and subtraction.
    Screenshot

Author

long399, e-mail: long399@mail.ru