1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Proengsoft\JsValidation\Javascript; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Facades\View; |
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use Proengsoft\JsValidation\Exceptions\PropertyNotFoundException; |
9
|
|
|
|
10
|
|
|
class JavascriptValidator implements Arrayable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Registered validator instance. |
14
|
|
|
* |
15
|
|
|
* @var \Proengsoft\JsValidation\Javascript\ValidatorHandler |
16
|
|
|
*/ |
17
|
|
|
protected $validator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Selector used in javascript generation. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $selector; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* View that renders Javascript. |
28
|
|
|
* |
29
|
|
|
* @var |
30
|
|
|
*/ |
31
|
|
|
protected $view; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Enable or disable remote validations. |
35
|
|
|
* |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
protected $remote; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* 'ignore' option for jQuery Validation Plugin. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $ignore; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ValidatorHandler $validator |
49
|
|
|
* @param array $options |
50
|
|
|
*/ |
51
|
192 |
|
public function __construct(ValidatorHandler $validator, $options = []) |
52
|
|
|
{ |
53
|
192 |
|
$this->validator = $validator; |
54
|
192 |
|
$this->setDefaults($options); |
55
|
192 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set default parameters. |
59
|
|
|
* |
60
|
|
|
* @param $options |
61
|
|
|
*/ |
62
|
192 |
|
protected function setDefaults($options) |
63
|
|
|
{ |
64
|
192 |
|
$this->selector = empty($options['selector']) ? 'form' : $options['selector']; |
65
|
192 |
|
$this->view = empty($options['view']) ? 'jsvalidation::bootstrap' : $options['view']; |
66
|
192 |
|
$this->remote = isset($options['remote']) ? $options['remote'] : true; |
67
|
192 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Render the specified view with validator data. |
71
|
|
|
* |
72
|
|
|
* @param \Illuminate\Contracts\View\View|string|null $view |
73
|
|
|
* @param string|null $selector |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
48 |
|
public function render($view = null, $selector = null) |
78
|
|
|
{ |
79
|
48 |
|
$this->view($view); |
80
|
48 |
|
$this->selector($selector); |
81
|
|
|
|
82
|
48 |
|
return View::make($this->view, ['validator' => $this->getViewData()]) |
83
|
36 |
|
->render(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the view data as an array. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
48 |
|
public function toArray() |
92
|
|
|
{ |
93
|
48 |
|
return $this->getViewData(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the string resulting of render default view. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
24 |
|
public function __toString() |
102
|
|
|
{ |
103
|
|
|
try { |
104
|
24 |
|
return $this->render(); |
105
|
12 |
|
} catch (Exception $exception) { |
106
|
12 |
|
return trigger_error($exception->__toString(), E_USER_ERROR); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gets value from view data. |
112
|
|
|
* |
113
|
|
|
* @param $name |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
* |
117
|
|
|
* @throws PropertyNotFoundException |
118
|
|
|
*/ |
119
|
24 |
|
public function __get($name) |
120
|
|
|
{ |
121
|
24 |
|
$data = $this->getViewData(); |
122
|
24 |
|
if (! array_key_exists($name, $data)) { |
123
|
12 |
|
throw new PropertyNotFoundException($name, get_class()); |
124
|
|
|
} |
125
|
|
|
|
126
|
12 |
|
return $data[$name]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Gets view data. |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
120 |
|
protected function getViewData() |
135
|
|
|
{ |
136
|
120 |
|
$this->validator->setRemote($this->remote); |
137
|
120 |
|
$data = $this->validator->validationData(); |
138
|
108 |
|
$data['selector'] = $this->selector; |
139
|
|
|
|
140
|
108 |
|
if (! is_null($this->ignore)) { |
141
|
12 |
|
$data['ignore'] = $this->ignore; |
142
|
8 |
|
} |
143
|
|
|
|
144
|
108 |
|
return $data; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the form selector to validate. |
149
|
|
|
* |
150
|
|
|
* @param string $selector |
151
|
|
|
* |
152
|
|
|
* @deprecated |
153
|
|
|
*/ |
154
|
|
|
public function setSelector($selector) |
155
|
|
|
{ |
156
|
|
|
$this->selector = $selector; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set the form selector to validate. |
161
|
|
|
* |
162
|
|
|
* @param string $selector |
163
|
|
|
* |
164
|
|
|
* @return JavascriptValidator |
165
|
|
|
*/ |
166
|
48 |
|
public function selector($selector) |
167
|
|
|
{ |
168
|
48 |
|
$this->selector = is_null($selector) ? $this->selector : $selector; |
169
|
|
|
|
170
|
48 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set the input selector to ignore for validation. |
175
|
|
|
* |
176
|
|
|
* @param string $ignore |
177
|
|
|
* |
178
|
|
|
* @return JavascriptValidator |
179
|
|
|
*/ |
180
|
12 |
|
public function ignore($ignore) |
181
|
|
|
{ |
182
|
12 |
|
$this->ignore = $ignore; |
183
|
|
|
|
184
|
12 |
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set the view to render Javascript Validations. |
189
|
|
|
* |
190
|
|
|
* @param \Illuminate\Contracts\View\View|string|null $view |
191
|
|
|
* |
192
|
|
|
* @return JavascriptValidator |
193
|
|
|
*/ |
194
|
48 |
|
public function view($view) |
195
|
|
|
{ |
196
|
48 |
|
$this->view = is_null($view) ? $this->view : $view; |
197
|
|
|
|
198
|
48 |
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Enables or disables remote validations. |
203
|
|
|
* |
204
|
|
|
* @param bool|null $enabled |
205
|
|
|
* |
206
|
|
|
* @return JavascriptValidator |
207
|
|
|
*/ |
208
|
12 |
|
public function remote($enabled = true) |
209
|
|
|
{ |
210
|
12 |
|
$this->remote = $enabled; |
211
|
|
|
|
212
|
12 |
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Validate Conditional Validations using Ajax in specified fields. |
217
|
|
|
* |
218
|
|
|
* @param string $attribute |
219
|
|
|
* @param string|array $rules |
220
|
|
|
* |
221
|
|
|
* @return JavascriptValidator |
222
|
|
|
*/ |
223
|
12 |
|
public function sometimes($attribute, $rules) |
224
|
|
|
{ |
225
|
12 |
|
$this->validator->sometimes($attribute, $rules); |
226
|
|
|
|
227
|
12 |
|
return $this; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|