//控制器SiteController
class SiteController extends Controller
{
// ...
public function actions()
{
return [
// ...
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
// ...
}
?>
定义表单Model
class ContactForm extends Model
{
// ...
public $verifyCode;
// ...
public function rules()
{
return [
// ...
['verifyCode', 'captcha'],
];
}
// ...
}
?>
在view中调用方法
$form = ActiveForm::begin(['id' => 'contact-form']); ?>
// ...
$form->field($model, 'verifyCode')->widget(Captcha::className()) ?>
// ...
ActiveForm::end(); ?>
yii2.0给表单添加验证码的实现方法
最新推荐文章于 2023-05-26 16:27:00 发布