EmailValidator::client()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php /** MicroEmailValidator */
2
3
namespace Micro\Validator;
4
5
use Micro\Form\IFormModel;
6
7
/**
8
 * EmailValidator class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Validator
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class EmailValidator extends BaseValidator
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function validate(IFormModel $model)
25
    {
26
        foreach ($this->elements AS $element) {
27
            if (!$model->checkAttributeExists($element)) {
28
                $this->errors[] = 'Parameter '.$element.' not defined in class '.get_class($model);
29
30
                return false;
31
            }
32
            if (!filter_var($model->$element, FILTER_VALIDATE_EMAIL)) {
33
                $this->errors[] = 'Parameter '.$element.' is not a valid E-mail address';
34
35
                return false;
36
            }
37
        }
38
39
        return true;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function client(IFormModel $model)
46
    {
47
        return 'if (/^[\w.-]{1,}@[\w.-]{1,}$/.test(this.value) != true) {'.
48
            ' e.preventDefault(); this.focus(); alert(\'Value is not a valid e-mail\'); }';
49
    }
50
}
51