MathCaptchaAction for Yii Framework 2.0
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.
- Addition.
- Subtraction.
- Multiplication.
Default addition or subtraction operation will be used randomly.
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' => ['-'],
],
...
];
}
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:
- Expression with multiplication.
- Expression with multiplication and addition.
- Expression with multiplication and subtraction.
long399, e-mail: [email protected]