AjaxValidationTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 18
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A performAjaxValidation() 0 8 3
1
<?php
2
3
namespace app\traits;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\helpers\Json;
8
use yii\web\Response;
9
use yii\widgets\ActiveForm;
10
11
/**
12
 * Class AjaxValidationTrait performs ajax validation.
13
 *
14
 * @author Igor Chepurnoy <[email protected]>
15
 *
16
 * @since 1.0
17
 */
18
trait AjaxValidationTrait
19
{
20
    /**
21
     * Performs ajax validation.
22
     *
23
     * @param Model $model
24
     *
25
     * @throws \yii\base\ExitException
26
     */
27
    protected function performAjaxValidation(Model $model)
28
    {
29
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
30
            Yii::$app->response->format = Response::FORMAT_JSON;
31
            echo Json::encode(ActiveForm::validate($model));
32
            Yii::$app->end();
33
        }
34
    }
35
}
36