1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Proengsoft\JsValidation\Javascript; |
4
|
|
|
|
5
|
|
|
trait JavascriptRulesTrait |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Handles multidimensional attribute names. |
10
|
|
|
* |
11
|
|
|
* @param string $attribute |
12
|
|
|
* |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
abstract protected function getAttributeName($attribute); |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Confirmed rule is applied to confirmed attribute. |
20
|
|
|
* |
21
|
|
|
* @param $attribute |
22
|
|
|
* @param array $parameters |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
protected function ruleConfirmed($attribute, array $parameters) |
27
|
|
|
{ |
28
|
|
|
$parameters[0] = $this->getAttributeName($attribute); |
29
|
|
|
$attribute = "{$attribute}_confirmation"; |
30
|
|
|
|
31
|
|
|
return [$attribute, $parameters]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns Javascript parameters for After rule. |
36
|
|
|
* |
37
|
|
|
* @param $attribute |
38
|
|
|
* @param array $parameters |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
protected function ruleAfter($attribute, array $parameters) |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
if (!($date = strtotime($parameters[0]))) { |
46
|
|
|
$date = $this->getAttributeName($parameters[0]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return [$attribute, [$date]]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns Javascript parameters for Before rule. |
54
|
|
|
* |
55
|
|
|
* @param $attribute |
56
|
|
|
* @param array $parameters |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
protected function ruleBefore($attribute, array $parameters) |
61
|
|
|
{ |
62
|
|
|
return $this->ruleAfter($attribute, $parameters); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Validate that two attributes match. |
67
|
|
|
* |
68
|
|
|
* @param string $attribute |
69
|
|
|
* @param array $parameters |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
protected function ruleSame($attribute, array $parameters) |
74
|
|
|
{ |
75
|
|
|
$other = $this->getAttributeName($parameters[0]); |
76
|
|
|
|
77
|
|
|
return [$attribute, [$other]]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Validate that an attribute is different from another attribute. |
82
|
|
|
* |
83
|
|
|
* @param string $attribute |
84
|
|
|
* @param array $parameters |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
protected function ruleDifferent($attribute, array $parameters) |
89
|
|
|
{ |
90
|
|
|
return $this->ruleSame($attribute, $parameters); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Validate that an attribute exists when any other attribute exists. |
95
|
|
|
* |
96
|
|
|
* @param string $attribute |
97
|
|
|
* @param mixed $parameters |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
protected function ruleRequiredWith($attribute, array $parameters) |
102
|
|
|
{ |
103
|
|
|
$parameters = array_map([$this, 'getAttributeName'], $parameters); |
104
|
|
|
|
105
|
|
|
return [$attribute, $parameters]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Validate that an attribute exists when all other attributes exists. |
110
|
|
|
* |
111
|
|
|
* @param string $attribute |
112
|
|
|
* @param mixed $parameters |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
protected function ruleRequiredWithAll($attribute, array $parameters) |
117
|
|
|
{ |
118
|
|
|
return $this->ruleRequiredWith($attribute, $parameters); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Validate that an attribute exists when another attribute does not. |
123
|
|
|
* |
124
|
|
|
* @param string $attribute |
125
|
|
|
* @param mixed $parameters |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
protected function ruleRequiredWithout($attribute, array $parameters) |
130
|
|
|
{ |
131
|
|
|
return $this->ruleRequiredWith($attribute, $parameters); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Validate that an attribute exists when all other attributes do not. |
136
|
|
|
* |
137
|
|
|
* @param string $attribute |
138
|
|
|
* @param mixed $parameters |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
protected function ruleRequiredWithoutAll($attribute, array $parameters) |
143
|
|
|
{ |
144
|
|
|
return $this->ruleRequiredWith($attribute, $parameters); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Validate that an attribute exists when another attribute has a given value. |
149
|
|
|
* |
150
|
|
|
* @param string $attribute |
151
|
|
|
* @param mixed $parameters |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
protected function ruleRequiredIf($attribute, array $parameters) |
156
|
|
|
{ |
157
|
|
|
$parameters[0] = $this->getAttributeName($parameters[0]); |
158
|
|
|
|
159
|
|
|
return [$attribute, $parameters]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
} |
164
|
|
|
|