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
|
|
|
* Create a new JsValidation instance. |
31
|
|
|
* |
32
|
|
|
* @param RuleParser $rules |
33
|
|
|
* @param MessageParser $messages |
34
|
|
|
*/ |
35
|
6 |
|
public function __construct(RuleParser $rules, MessageParser $messages) |
36
|
|
|
{ |
37
|
6 |
|
$this->rules = $rules; |
38
|
6 |
|
$this->messages = $messages; |
39
|
6 |
|
$this->validator = $rules->getDelegatedValidator(); |
40
|
6 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Sets delegated Validator instance. |
44
|
|
|
* |
45
|
|
|
* @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator |
46
|
|
|
*/ |
47
|
2 |
|
public function setDelegatedValidator(DelegatedValidator $validator) |
48
|
|
|
{ |
49
|
2 |
|
$this->validator = $validator; |
50
|
2 |
|
$this->rules->setDelegatedValidator($validator); |
51
|
2 |
|
$this->messages->setDelegatedValidator($validator); |
52
|
2 |
|
} |
53
|
|
|
|
54
|
2 |
|
protected function generateJavascriptValidations($includeRemote = true) |
55
|
|
|
{ |
56
|
2 |
|
$jsValidations = array(); |
57
|
|
|
|
58
|
2 |
|
foreach ($this->validator->getRules() as $attribute => $rules) { |
59
|
2 |
|
if (! $this->jsValidationEnabled($attribute)) { |
60
|
1 |
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$newRules = $this->jsConvertRules($attribute, $rules, $includeRemote); |
64
|
1 |
|
$jsValidations = array_merge($jsValidations, $newRules); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
2 |
|
return $jsValidations; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Make Laravel Validations compatible with JQuery Validation Plugin. |
72
|
|
|
* |
73
|
|
|
* @param $attribute |
74
|
|
|
* @param $rules |
75
|
|
|
* @param $includeRemote |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
1 |
|
protected function jsConvertRules($attribute, $rules, $includeRemote) |
80
|
|
|
{ |
81
|
1 |
|
$jsRules = []; |
82
|
1 |
|
foreach ($rules as $rawRule) { |
83
|
1 |
|
list($rule, $parameters) = $this->validator->parseRule($rawRule); |
84
|
1 |
|
list($jsAttribute, $jsRule, $jsParams) = $this->rules->getRule($attribute, $rule, $parameters); |
85
|
1 |
|
if ($this->isValidatable($jsRule, $includeRemote)) { |
86
|
1 |
|
$jsRules[$jsAttribute][$jsRule][] = array($rule, $jsParams, |
87
|
1 |
|
$this->messages->getMessage($attribute, $rule, $parameters), |
88
|
1 |
|
$this->validator->isImplicit($rule), |
89
|
|
|
); |
90
|
1 |
|
} |
91
|
1 |
|
} |
92
|
|
|
|
93
|
1 |
|
return $jsRules; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check if rule should be validated with javascript. |
98
|
|
|
* |
99
|
|
|
* @param $jsRule |
100
|
|
|
* @param $includeRemote |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
1 |
|
protected function isValidatable($jsRule, $includeRemote) |
104
|
|
|
{ |
105
|
1 |
|
return $jsRule && ($includeRemote || $jsRule !== RuleParser::REMOTE_RULE); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Check if JS Validation is disabled for attribute. |
110
|
|
|
* |
111
|
|
|
* @param $attribute |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
2 |
|
public function jsValidationEnabled($attribute) |
116
|
|
|
{ |
117
|
2 |
|
return ! $this->validator->hasRule($attribute, self::JSVALIDATION_DISABLE); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns view data to render javascript. |
122
|
|
|
* |
123
|
|
|
* @param bool $remote |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
2 |
|
public function validationData($remote = true) |
127
|
|
|
{ |
128
|
2 |
|
$jsMessages = array(); |
129
|
2 |
|
$jsValidations = $this->generateJavascriptValidations($remote); |
130
|
|
|
|
131
|
|
|
return [ |
132
|
2 |
|
'rules' => $jsValidations, |
133
|
2 |
|
'messages' => $jsMessages, |
134
|
2 |
|
]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|