1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Proengsoft\JsValidation\Javascript; |
4
|
|
|
|
5
|
|
|
use Illuminate\Validation\Validator; |
6
|
|
|
use Proengsoft\JsValidation\Support\DelegatedValidator; |
7
|
|
|
use Proengsoft\JsValidation\Support\UseDelegatedValidatorTrait; |
8
|
|
|
|
9
|
|
|
class ValidatorHandler |
10
|
|
|
{ |
11
|
|
|
use UseDelegatedValidatorTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Rule used to disable validations. |
15
|
|
|
* |
16
|
|
|
* @const string |
17
|
|
|
*/ |
18
|
|
|
const JSVALIDATION_DISABLE = 'NoJsValidation'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var RuleParser |
22
|
|
|
*/ |
23
|
|
|
protected $rules; |
24
|
|
|
/** |
25
|
|
|
* @var MessageParser |
26
|
|
|
*/ |
27
|
|
|
protected $messages; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $remote = true; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a new JsValidation instance. |
36
|
|
|
* |
37
|
|
|
* @param RuleParser $rules |
38
|
|
|
* @param MessageParser $messages |
39
|
|
|
*/ |
40
|
10 |
|
public function __construct(RuleParser $rules, MessageParser $messages) |
41
|
|
|
{ |
42
|
10 |
|
$this->rules = $rules; |
43
|
10 |
|
$this->messages = $messages; |
44
|
10 |
|
$this->validator = $rules->getDelegatedValidator(); |
45
|
10 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Sets delegated Validator instance. |
49
|
|
|
* |
50
|
|
|
* @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator |
51
|
|
|
*/ |
52
|
4 |
|
public function setDelegatedValidator(DelegatedValidator $validator) |
53
|
|
|
{ |
54
|
4 |
|
$this->validator = $validator; |
55
|
4 |
|
$this->rules->setDelegatedValidator($validator); |
56
|
4 |
|
$this->messages->setDelegatedValidator($validator); |
57
|
4 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Enable or disables remote validations. |
61
|
|
|
* |
62
|
|
|
* @param bool $enabled |
63
|
|
|
*/ |
64
|
1 |
|
public function setRemote($enabled) |
65
|
|
|
{ |
66
|
1 |
|
$this->remote = $enabled; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Generate Javascript Validations. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
4 |
|
protected function generateJavascriptValidations() |
75
|
|
|
{ |
76
|
4 |
|
$jsValidations = array(); |
77
|
|
|
|
78
|
4 |
|
foreach ($this->validator->getRules() as $attribute => $rules) { |
79
|
4 |
|
if (! $this->jsValidationEnabled($attribute)) { |
80
|
1 |
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
$newRules = $this->jsConvertRules($attribute, $rules, $this->remote); |
84
|
3 |
|
$jsValidations = array_merge($jsValidations, $newRules); |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
return $jsValidations; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Make Laravel Validations compatible with JQuery Validation Plugin. |
92
|
|
|
* |
93
|
|
|
* @param $attribute |
94
|
|
|
* @param $rules |
95
|
|
|
* @param bool $includeRemote |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
3 |
|
protected function jsConvertRules($attribute, $rules, $includeRemote) |
100
|
|
|
{ |
101
|
3 |
|
$jsRules = []; |
102
|
3 |
|
foreach ($rules as $rawRule) { |
103
|
3 |
|
list($rule, $parameters) = $this->validator->parseRule($rawRule); |
104
|
3 |
|
list($jsAttribute, $jsRule, $jsParams) = $this->rules->getRule($attribute, $rule, $parameters, $rawRule); |
105
|
3 |
|
if ($this->isValidatable($jsRule, $includeRemote)) { |
106
|
2 |
|
$jsRules[$jsAttribute][$jsRule][] = array($rule, $jsParams, |
107
|
2 |
|
$this->messages->getMessage($attribute, $rule, $parameters), |
108
|
3 |
|
$this->validator->isImplicit($rule), |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
return $jsRules; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check if rule should be validated with javascript. |
118
|
|
|
* |
119
|
|
|
* @param $jsRule |
120
|
|
|
* @param bool $includeRemote |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
3 |
|
protected function isValidatable($jsRule, $includeRemote) |
124
|
|
|
{ |
125
|
3 |
|
return $jsRule && ($includeRemote || $jsRule !== RuleParser::REMOTE_RULE); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Check if JS Validation is disabled for attribute. |
130
|
|
|
* |
131
|
|
|
* @param $attribute |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
4 |
|
public function jsValidationEnabled($attribute) |
136
|
|
|
{ |
137
|
4 |
|
return ! $this->validator->hasRule($attribute, self::JSVALIDATION_DISABLE); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns view data to render javascript. |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
4 |
|
public function validationData() |
146
|
|
|
{ |
147
|
4 |
|
$jsMessages = array(); |
148
|
4 |
|
$jsValidations = $this->generateJavascriptValidations(); |
149
|
|
|
|
150
|
|
|
return [ |
151
|
4 |
|
'rules' => $jsValidations, |
152
|
4 |
|
'messages' => $jsMessages, |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Validate Conditional Validations using Ajax in specified fields. |
158
|
|
|
* |
159
|
|
|
* @param string $attribute |
160
|
|
|
* @param string|array $rules |
161
|
|
|
*/ |
162
|
|
|
public function sometimes($attribute, $rules = []) |
163
|
|
|
{ |
164
|
1 |
|
$callback = function () { |
165
|
|
|
return true; |
166
|
1 |
|
}; |
167
|
1 |
|
$this->validator->sometimes($attribute, $rules, $callback); |
168
|
1 |
|
$this->rules->addConditionalRules($attribute, (array) $rules); |
169
|
1 |
|
} |
170
|
|
|
} |
171
|
|
|
|