yii asynchronous custom validation and verification methods


First, the asynchronous verification, ajax generally used to verify the uniqueness more
1, model validation turned 
[[ 'Mobile_ID', 'IP'], 'UNIQUE', 'Message' => Yii :: T ( 'App', 'E10010')],
2, the controller authentication method
// ajax asynchronous The only verification
if (Yii :: $ app-> request- > isAjax && Yii :: $ app-> request-> isPost && $ model-> load (Yii :: $ app-> request-> post ())) {
:: $ APP- Yii> Response-> the format :: = the Response FORMAT_JSON;
$ = Yii :: $ APP- the params> request-> POST ( 'CardNumber');
$ valiArr = [ 'Mobile_ID']; // according to some Analyzing conditions, which fields a request to verify the
return the validate the ActiveForm :: (Model $, $ valiArr);
}
. 3, view layer enables asynchronous validation
field (Model $, 'Mobile_ID', [<= $ form-?>
'enableAjaxValidation to be '=> to true,
]);
?>


Second, the custom validation method
call to $ models-> method validation will trigger custom fields when validate () method
model layer
['idinfobind', 'checkIdInfoBind', 'on' => 'add'],
public function checkIdInfoBind($attribute)
{
$where = Yii::$app->request->post()['Unit']['idinfobind'];
$namedata = Top::find()->where(['id' => $where])->one();
// $studentiddata = Top::find()->where(['student_id' => $where])->one();
if (empty($namedata) ) {
$this->addError($attribute, Yii::t('app', 'user id info bind failed'));//添加错误信息
}

}

Guess you like

Origin www.cnblogs.com/fwqblogs/p/11077835.html