AjaxValidationTrait.php 761 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace common\modules\user\traits;
  3. use yii\base\Model;
  4. use yii\web\Response;
  5. use yii\widgets\ActiveForm;
  6. /**
  7. * @author Dmitry Erofeev <dmeroff@gmail.com>
  8. */
  9. trait AjaxValidationTrait
  10. {
  11. /**
  12. * Performs ajax validation.
  13. *
  14. * @param Model $model
  15. *
  16. * @throws \yii\base\ExitException
  17. */
  18. public function performAjaxValidation(Model $model)
  19. {
  20. if (\Yii::$app->request->isAjax && $model->load(\Yii::$app->request->post()) && \Yii::$app->request->get('validate', '0') == '1') {
  21. \Yii::$app->response->format = Response::FORMAT_JSON;
  22. \Yii::$app->response->data = ActiveForm::validate($model);
  23. \Yii::$app->response->send();
  24. \Yii::$app->end();
  25. }
  26. }
  27. }