1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Fiv\Form; |
4
|
|
|
|
5
|
|
|
use Fiv\Form\Form; |
6
|
|
|
use Fiv\Form\Validator\CallBackValidator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @package Tests\Form\Form |
10
|
|
|
*/ |
11
|
|
|
class ValidatorsTest extends \Tests\Fiv\Form\FormTestCase { |
12
|
|
|
|
13
|
|
|
public function testCallback() { |
14
|
|
|
$lengthValidator = new CallBackValidator(function ($value) { |
15
|
|
|
return strlen($value) > 3 and strlen($value) < 10; |
16
|
|
|
}); |
17
|
|
|
|
18
|
|
|
$form = new Form(); |
19
|
|
|
$form->input('login') |
20
|
|
|
->addValidator($lengthValidator); |
21
|
|
|
|
22
|
|
|
$form->setData([ |
23
|
|
|
$form->getUid() => 1, |
24
|
|
|
'login' => 'testLogin', |
25
|
|
|
]); |
26
|
|
|
|
27
|
|
|
$this->assertTrue($form->isValid()); |
28
|
|
|
$this->assertFalse($lengthValidator->hasErrors()); |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
$form->setData([ |
34
|
|
|
$form->getUid() => 1, |
35
|
|
|
'login' => 'tes', |
36
|
|
|
]); |
37
|
|
|
|
38
|
|
|
$this->assertFalse($form->isValid()); |
39
|
|
|
$this->assertTrue($lengthValidator->hasErrors()); |
40
|
|
|
|
41
|
|
|
$form->setData([ |
42
|
|
|
$form->getUid() => 1, |
43
|
|
|
'login' => 'testtesttesttesttesttesttesttest', |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
$this->assertFalse($form->isValid()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
|
public function testRegexp() { |
53
|
|
|
$regexpValidator = new \Fiv\Form\Validator\Regexp(); |
54
|
|
|
$regexpValidator->setRegexp('![^\@]+\@[^\@]+!'); |
55
|
|
|
|
56
|
|
|
$form = new Form(); |
57
|
|
|
$form->input('email') |
58
|
|
|
->addValidator($regexpValidator); |
59
|
|
|
|
60
|
|
|
$form->setData([ |
61
|
|
|
$form->getUid() => 1, |
62
|
|
|
'email' => 'test@test', |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$this->assertTrue($form->isValid()); |
66
|
|
|
|
67
|
|
|
$form->setData([ |
68
|
|
|
$form->getUid() => 1, |
69
|
|
|
'email' => 'test', |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$this->assertFalse($form->isValid()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
public function testIn() { |
77
|
|
|
$inValidator = new \Fiv\Form\Validator\In(); |
78
|
|
|
$inValidator->setValues(['a', 'b', 'c']); |
79
|
|
|
|
80
|
|
|
$form = new Form(); |
81
|
|
|
$form->input('inputName') |
82
|
|
|
->addValidator($inValidator); |
83
|
|
|
|
84
|
|
|
$form->setData([ |
85
|
|
|
$form->getUid() => 1, |
86
|
|
|
'inputName' => 'a', |
87
|
|
|
]); |
88
|
|
|
$this->assertTrue($form->isValid()); |
89
|
|
|
|
90
|
|
|
$form->setData([ |
91
|
|
|
$form->getUid() => 1, |
92
|
|
|
'inputName' => 'd', |
93
|
|
|
]); |
94
|
|
|
$this->assertFalse($form->isValid()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
public function testCallBackValidatorEmail() { |
99
|
|
|
$form = new Form(); |
100
|
|
|
|
101
|
|
|
$existEmailList = [ |
102
|
|
|
'[email protected]', |
103
|
|
|
'[email protected]', |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$callBackValidator = (new CallBackValidator(function ($value) use ($existEmailList) { |
107
|
|
|
if (empty($value)) { |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
if (in_array($value, $existEmailList)) { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
return true; |
114
|
|
|
}))->setErrorMessage('Email already exist!'); |
115
|
|
|
|
116
|
|
|
$input = $form->input('email'); |
117
|
|
|
$input->addValidator($callBackValidator); |
118
|
|
|
|
119
|
|
|
$form->setData([ |
120
|
|
|
$form->getUid() => 1, |
121
|
|
|
'email' => '[email protected]', |
122
|
|
|
]); |
123
|
|
|
|
124
|
|
|
$this->assertFalse($form->isValid()); |
125
|
|
|
$this->assertEquals('Email already exist!', $callBackValidator->getFirstError()); |
126
|
|
|
|
127
|
|
|
$form->setData([ |
128
|
|
|
$form->getUid() => 1, |
129
|
|
|
'email' => '[email protected]', |
130
|
|
|
]); |
131
|
|
|
|
132
|
|
|
$this->assertTrue($form->isValid()); |
133
|
|
|
} |
134
|
|
|
} |