simialbi/yii2-visualcaptcha

Visual captcha implementation for yii2 framework

Installs: 301

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 2

Forks: 2

Open Issues: 0

Language:JavaScript

Type:yii2-extension

2.0.0 2024-03-22 14:24 UTC

This package is auto-updated.

Last update: 2024-11-22 15:57:38 UTC


README

Resources

Installation

The preferred way to install this extension is through composer.

Either run

$ php composer.phar require --prefer-dist simialbi/yii2-visualcaptcha

or add

"simialbi/yii2-visualcaptcha": "*"

to the require section of your composer.json

Usage

Setup Module

Add the module visualcaptcha to the modules and bootstrap section of your configuration file:

[
    'bootstrap' => ['log', 'visualcaptcha'],
    'modules' => [
        'visualcaptcha' => [
            'class' => 'simialbi\yii2\visualcaptcha\Module'
        ]
    ]
]

Example Usage

Define Model

<?php
namespace app\models;

use simialbi\yii2\visualcaptcha\validators\VisualCaptchaValidator;
use yii\base\Model;

class MyForm extends Model {
	public $name;
	public $email;
	public $subject;
	public $body;
	public $verifyCode;

	/**
	 * {@inheritdoc}
	 */
	public function rules() {
		return [
			[['name', 'email', 'subject', 'body', 'verifyCode'], 'required'],
			['email', 'email'],
			['verifyCode', VisualCaptchaValidator::class]
		];
	}
}

Use in view

<?php

use simialbi\yii2\visualcaptcha\widgets\VisualCaptcha;
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model app\models\MyForm */

$this->title = 'Visual Captcha';
$this->params['breadcrumbs'][] = $this->title;

?>
<div class="my-visualcaptcha">
    <?php $form = ActiveForm::begin(['id' => 'test-form']); ?>
	
    <?=$form->field($model, 'name')->textInput(['autofocus' => true]);?>
    <?=$form->field($model, 'email');?>
    <?=$form->field($model, 'subject');?>
    <?=$form->field($model, 'body')->textarea(['rows' => 6]);?>
    <?=$form->field($model, 'verifyCode')->widget(VisualCaptcha::class, [
        'numberOfImages' => 10,
        'clientOptions' => [
            // change templates
            'templates' => [
                'button' => '
                    <div class="visualCaptcha-{class}-button">
                        <a href="javascript:;">
                            <img src="{path}{class}{retinaExtra}.png" title="{classTitle}" alt="{classAlt}">
                        </a>
                    </div>',
                'buttonGroup' => '
                     <div class="visualCaptcha-button-group">
                         {btnRefresh}
                         {btnAccessibility}
                     </div>'
            ]
        ]
    ]);?>

    <div class="form-group">
        <?=Html::submitButton('Submit', ['class' => ['btn', 'btn-primary']]);?>
    </div>

    <?php ActiveForm::end(); ?>
?>
</div>

License

yii2-visualcaptcha is released under MIT license. See bundled LICENSE for details.