Completed
Push — 2.1 ( 75349f...bf116e )
by Alexander
29:27
created

EmailValidator::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 4
crap 6
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\jquery\validators\client;
9
10
use yii\helpers\Json;
11
use yii\jquery\PunycodeAsset;
12
use yii\jquery\ValidationAsset;
13
use yii\validators\client\ClientValidator;
14
use yii\web\JsExpression;
15
16
/**
17
 * EmailValidator composes client-side validation code from [[\yii\validators\EmailValidator]].
18
 *
19
 * @see \yii\validators\EmailValidator
20
 * @see ValidationAsset
21
 *
22
 * @author Paul Klimov <[email protected]>
23
 * @since 2.1.0
24
 */
25
class EmailValidator extends ClientValidator
26
{
27
    /**
28
     * @inheritdoc
29
     */
30
    public function build($validator, $model, $attribute, $view)
31
    {
32
        /* @var $validator \yii\validators\EmailValidator */
33
        ValidationAsset::register($view);
34
        if ($validator->enableIDN) {
35
            PunycodeAsset::register($view);
36
        }
37
        $options = $this->getClientOptions($validator, $model, $attribute);
38
        return 'yii.validation.email(value, messages, ' . Json::htmlEncode($options) . ');';
39
    }
40
41
    /**
42
     * Returns the client-side validation options.
43
     * @param \yii\validators\EmailValidator $validator the server-side validator.
44
     * @param \yii\base\Model $model the model being validated
45
     * @param string $attribute the attribute name being validated
46
     * @return array the client-side validation options
47
     */
48
    public function getClientOptions($validator, $model, $attribute)
49
    {
50
        $options = [
51
            'pattern' => new JsExpression($validator->pattern),
52
            'fullPattern' => new JsExpression($validator->fullPattern),
53
            'allowName' => $validator->allowName,
54
            'message' => $validator->formatMessage($validator->message, [
55
                'attribute' => $model->getAttributeLabel($attribute),
56
            ]),
57
            'enableIDN' => (bool)$validator->enableIDN,
58
        ];
59
        if ($validator->skipOnEmpty) {
60
            $options['skipOnEmpty'] = 1;
61
        }
62
63
        return $options;
64
    }
65
}