mumunotes

  • Home
  • Contact Us

Friday, 12 June 2015

Implementation Captcha on Yii2

 mumunotesss     09:35     Yii, Yii2     No comments   

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:

namespace app\models;

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
}
The second, open your login.php file on views/site directory and change with following the code bellow.
//Another declaration class.
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();
Now, we will trial the yii2basic application. Please open your browser and access this link "http://localhost/yii2basic/web/index.php?r=site/login"

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...


  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

About Me

mumunotesss
View my complete profile

Popular Posts

  • Change password with validation and repeat password on Yii2
    Hello everybody. I could not post an article because I didn't have new knowledge to share. Ok, this day I will share about "...
  • Import Data from CSV File to Database Without Extension in Yii2
    Hello everybody, Assalamualaikum Wr. Wb. Nearly 1 month I don't publish article. Now, I publish an article about " Import Data fro...
  • Membuat login Yii2 dengan Database
    Selamat pagi guys. Sorry beberapa hari kamaren sibuk. Jadi belum sempet update blog lagi. Mumpung kali ini sempet, hari ini mau berbagi te...
  • Membuat fungsi CRUD dengan Gii Yii2
    Selamat pagi guys. Setelah kemaren kita membuat login dari database, hari ini kita akan membuat CRUD (create, read, update, delete) dengan ...

Blog Archive

  • ▼  2015 (16)
    • ►  November (1)
    • ►  September (1)
    • ►  August (1)
    • ▼  June (8)
      • Upload File or Image on Yii2
      • Implementation google reCAPTCHA on Yii2 Applicati...
      • Implementation Captcha on Yii2
      • Change password with validation and repeat passwor...
      • Membuat Alert atau Notifikasi di Yii2
      • ACF (Access Control Filter) Yii2 dengan Role dari ...
      • Membuat fungsi CRUD dengan Gii Yii2
      • Membuat login Yii2 dengan Database
    • ►  May (5)

Translate

Copyright © mumunotes
Distributed By My Blogger Themes | Blogger Theme By NewBloggerThemes