forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Between.zep
102 lines (90 loc) · 2.41 KB
/
Between.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
97
98
99
100
101
102
/**
* 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\Between
*
* Validates that a value is between an inclusive range of two values.
* For a value x, the test is passed if minimum<=x<=maximum.
*
* <code>
* use Phalcon\Validation;
* use Phalcon\Validation\Validator\Between;
*
* $validator = new Validation();
*
* $validator->add(
* "price",
* new Between(
* [
* "minimum" => 0,
* "maximum" => 100,
* "message" => "The price must be between 0 and 100",
* ]
* )
* );
*
* $validator->add(
* [
* "price",
* "amount",
* ],
* new Between(
* [
* "minimum" => [
* "price" => 0,
* "amount" => 0,
* ],
* "maximum" => [
* "price" => 100,
* "amount" => 50,
* ],
* "message" => [
* "price" => "The price must be between 0 and 100",
* "amount" => "The amount must be between 0 and 50",
* ],
* ]
* )
* );
* </code>
*/
class Between extends Validator
{
protected template = "Field :field must be within the range of :min to :max";
/**
* Executes the validation
*/
public function validate(<Validation> validation, var field) -> bool
{
var value, minimum, maximum, replacePairs;
let value = validation->getValue(field),
minimum = this->getOption("minimum"),
maximum = this->getOption("maximum");
if typeof minimum == "array" {
let minimum = minimum[field];
}
if typeof maximum == "array" {
let maximum = maximum[field];
}
if value < minimum || value > maximum {
let replacePairs = [
":min": minimum,
":max": maximum
];
validation->appendMessage(
this->messageFactory(validation, field, replacePairs)
);
return false;
}
return true;
}
}