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