Speaktech.in

yii2 simple math captcha implementation


Model validation rules

publicfunction rules(){return[// name, email, subject and body are required[['name','email','subject','body','verifyCode'],'required'],// email has to be a valid email address['email','email'],// verifyCode needs to be entered correctly['verifyCode','captcha','captchaAction'=>'/registration/default/captcha'],];}


ActiveForm in view file

<?php $form =ActiveForm::begin(['id'=>'contact-form']);?><?= $form->field($model,'name')?><?= $form->field($model,'email')?><?= $form->field($model,'subject')?><?= $form->field($model,'body')->textArea(['rows'=>6])?><?= $form->field($model,'verifyCode')->widget(Captcha::className(),['captchaAction'=>'default/captcha','template'=>'<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',])?><divclass="form-group"><?=Html::submitButton('Submit',['class'=>'btn btn-primary','name'=>'contact-button'])?></div><?php ActiveForm::end();?>