1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Form\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\Form\MainTestCase { |
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
|
|
|
public function testRequire() { |
51
|
|
|
$form = new Form(); |
52
|
|
|
$form->input('login') |
53
|
|
|
->addValidator(new \Fiv\Form\Validator\Required()); |
54
|
|
|
|
55
|
|
|
$form->setData([ |
56
|
|
|
$form->getUid() => 1, |
57
|
|
|
'login' => 'testLogin', |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$this->assertTrue($form->isValid()); |
61
|
|
|
|
62
|
|
|
$form->setData([ |
63
|
|
|
$form->getUid() => 1, |
64
|
|
|
'login' => '', |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
$this->assertFalse($form->isValid()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
public function testRegexp() { |
72
|
|
|
$regexpValidator = new \Fiv\Form\Validator\Regexp(); |
73
|
|
|
$regexpValidator->setRegexp('![^\@]+\@[^\@]+!'); |
74
|
|
|
|
75
|
|
|
$form = new Form(); |
76
|
|
|
$form->input('email') |
77
|
|
|
->addValidator($regexpValidator); |
78
|
|
|
|
79
|
|
|
$form->setData([ |
80
|
|
|
$form->getUid() => 1, |
81
|
|
|
'email' => 'test@test', |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$this->assertTrue($form->isValid()); |
85
|
|
|
|
86
|
|
|
$form->setData([ |
87
|
|
|
$form->getUid() => 1, |
88
|
|
|
'email' => 'test', |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
$this->assertFalse($form->isValid()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
public function testIn() { |
96
|
|
|
$inValidator = new \Fiv\Form\Validator\In(); |
97
|
|
|
$inValidator->setValues(['a', 'b', 'c']); |
98
|
|
|
|
99
|
|
|
$form = new Form(); |
100
|
|
|
$form->input('inputName') |
101
|
|
|
->addValidator($inValidator); |
102
|
|
|
|
103
|
|
|
$form->setData([ |
104
|
|
|
$form->getUid() => 1, |
105
|
|
|
'inputName' => 'a', |
106
|
|
|
]); |
107
|
|
|
$this->assertTrue($form->isValid()); |
108
|
|
|
|
109
|
|
|
$form->setData([ |
110
|
|
|
$form->getUid() => 1, |
111
|
|
|
'inputName' => 'd', |
112
|
|
|
]); |
113
|
|
|
$this->assertFalse($form->isValid()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
public function testCallBackValidatorEmail() { |
118
|
|
|
$form = new Form(); |
119
|
|
|
|
120
|
|
|
$existEmailList = [ |
121
|
|
|
'[email protected]', |
122
|
|
|
'[email protected]', |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
$callBackValidator = (new CallBackValidator(function ($value) use ($existEmailList) { |
126
|
|
|
if (empty($value)) { |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
if (in_array($value, $existEmailList)) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
return true; |
133
|
|
|
}))->setErrorMessage('Email already exist!'); |
134
|
|
|
|
135
|
|
|
$input = $form->input('email'); |
136
|
|
|
$input->addValidator($callBackValidator); |
137
|
|
|
|
138
|
|
|
$form->setData([ |
139
|
|
|
$form->getUid() => 1, |
140
|
|
|
'email' => '[email protected]', |
141
|
|
|
]); |
142
|
|
|
|
143
|
|
|
$this->assertFalse($form->isValid()); |
144
|
|
|
$this->assertEquals('Email already exist!', $callBackValidator->getFirstError()); |
145
|
|
|
|
146
|
|
|
$form->setData([ |
147
|
|
|
$form->getUid() => 1, |
148
|
|
|
'email' => '[email protected]', |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
$this->assertTrue($form->isValid()); |
152
|
|
|
} |
153
|
|
|
} |