Yii
2
Вклад в тег
trait AjaxValidationTrait
{
/**
* Performs ajax validation.
*
* @param Model $model
*
* @throws \yii\base\ExitException
*/
protected function performAjaxValidation(Model $model)
{
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
echo json_encode(ActiveForm::validate($model));
Yii::$app->end();
}
}
}
class DefaultController extends BaseAdminController
{
use AjaxValidationTrait;
public function actionCreate()
{
$model = new Point();
$this->performAjaxValidation($model);
if ($model->validate()) {
....
} else {
return $this->render('create', [
'model' => $model]
);
}
}
}
<?php $form = ActiveForm::begin([
'enableAjaxValidation' => true,
'enableClientValidation' => false,
]); ?>