Assalamualaikum Wr. Wb. Hello everybody. Meet again with me in my blog. This session, I will share tutorial how to implementation captcha on Yii2. Let's start this session.
On this session, we still using yii2basic application that have been made a few days ago.
First, please open your LoginForm model class in models directory and change with following the code bellow:
Try with incorrect captcha

Try with correct captcha.
It's easy, isn't? Yii framework is amazing and it's easy. I hope this tutorial can help you more. So good luck...Thank you and Wassalamualaikum Wr. Wb...
On this session, we still using yii2basic application that have been made a few days ago.
First, please open your LoginForm model class in models directory and change with following the code bellow:
namespace app\models;The second, open your login.php file on views/site directory and change with following the code bellow.
use Yii;
use yii\base\Model;
/**
* LoginForm is the model behind the login form.
*/
class LoginForm extends Model
{
//Another variables
public $captcha; // add this varible to your model class.
/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['username', 'password'], 'required'],
['rememberMe', 'boolean'],
['captcha','captcha'], // add this code to your rules.
['password', 'validatePassword'],
];
}
//Another functions
}
//Another declaration class.Now, we will trial the yii2basic application. Please open your browser and access this link "http://localhost/yii2basic/web/index.php?r=site/login"
use yii\captcha\Captcha;
$form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "{label}\n{input}\n{error}",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
],
]);
//Another codes
$form->field($model, 'password')->passwordInput();
$form->field($model, 'captcha')->widget(Captcha::classname(), [
'template' => '{image}{input}',
]); //this code showing captcha input to end users.
//Another codes.
ActiveForm::end();
Try with incorrect captcha

Try with correct captcha.
It's easy, isn't? Yii framework is amazing and it's easy. I hope this tutorial can help you more. So good luck...Thank you and Wassalamualaikum Wr. Wb...