forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Identical.zep
96 lines (84 loc) · 2.16 KB
/
Identical.zep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
namespace Phalcon\Validation\Validator;
use Phalcon\Messages\Message;
use Phalcon\Validation;
use Phalcon\Validation\Validator;
/**
* Phalcon\Validation\Validator\Identical
*
* Checks if a value is identical to other
*
* <code>
* use Phalcon\Validation;
* use Phalcon\Validation\Validator\Identical;
*
* $validator = new Validation();
*
* $validator->add(
* "terms",
* new Identical(
* [
* "accepted" => "yes",
* "message" => "Terms and conditions must be accepted",
* ]
* )
* );
*
* $validator->add(
* [
* "terms",
* "anotherTerms",
* ],
* new Identical(
* [
* "accepted" => [
* "terms" => "yes",
* "anotherTerms" => "yes",
* ],
* "message" => [
* "terms" => "Terms and conditions must be accepted",
* "anotherTerms" => "Another terms must be accepted",
* ],
* ]
* )
* );
* </code>
*/
class Identical extends Validator
{
protected template = "Field :field does not have the expected value";
/**
* Executes the validation
*/
public function validate(<Validation> validation, var field) -> bool
{
var value, accepted;
bool valid;
let value = validation->getValue(field);
if this->hasOption("accepted") {
let accepted = this->getOption("accepted");
} elseif this->hasOption("value") {
let accepted = this->getOption("value");
}
if accepted {
if typeof accepted == "array" {
let accepted = accepted[field];
}
let valid = value == accepted;
}
if !valid {
validation->appendMessage(
this->messageFactory(validation, field)
);
return false;
}
return true;
}
}