The example in this article describes the simple implementation of the Yii2 method to add a validation code to a form. Share to everyone for your reference, specific as follows:
Controller Sitecontroller:
Class Sitecontroller extends Controller
{
//...
Public function actions ()
{return
[
//...
] Captcha ' => [
' class ' => ' yii\captcha\captchaaction ',
' Fixedverifycode ' => yii_env_test? ' Testme ': null,
],
];
}
// ...
}
? >
Define form Model:
Class Contactform extends Model
{
//...
public $verifyCode;
// ...
Public function rules ()
{return
[
//...
] [' Verifycode ', ' captcha '],
];
}
// ...
}
? >
To call a method in view:
$form = Activeform::begin ([' id ' => ' contact-form ']);?>
//...
$form->field ($model, ' Verifycode ')->widget (Captcha::classname ())?>
//...
Activeform::end ();?>
For more information on YII-related content, readers who are interested in this site can view the topics: Introduction to YII Framework and summary of common skills, "Summary of PHP Excellent development framework", "Smarty Template Introductory Course", "Introduction to PHP object-oriented programming", "PHP string" Summary of Usage , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on the YII framework.