本文實例講述了Yii2簡單實現給表單添加驗證碼的方法。分享給大家供大家參考,具體如下:
控制器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(); ?>
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選