@@ -71,6 +71,11 @@ trait ValidationTrait
71
71
*/
72
72
private $ _hasValidated = false ;
73
73
74
+ private $ _position = [
75
+ 'attr ' => 0 ,
76
+ 'validator ' => 1 ,
77
+ ];
78
+
74
79
/**
75
80
* @return array
76
81
*/
@@ -215,37 +220,43 @@ public function validate(array $onlyChecked = [], $hasErrorStop=false)
215
220
*/
216
221
protected function doValidate ($ data , $ name , $ validator , $ copy )
217
222
{
218
- $ result = ValidatorList::required ($ data , $ name );
223
+ if ( !isset ($ data [$ name ]) ) {
224
+ return [false , $ validator instanceof \Closure ? 'callback ' : $ validator ];
225
+ }
219
226
220
- if ($ result && $ validator !== 'required ' ) {
221
- array_unshift ( $ copy , $ data [ $ name]); // 压入当前属性值
227
+ if ( $ validator === 'required ' ) {
228
+ $ result = ValidatorList:: required ( $ data , $ name);
222
229
223
- // if it's a closure
224
- if ( is_callable ($ validator ) && $ validator instanceof \Closure) {
225
- $ callback = $ validator ;
226
- $ validator = 'callback ' ;
230
+ return [$ result ,$ validator ];
231
+ }
227
232
228
- // if it is a custom add callback in the property {@see $_validators}.
229
- } elseif ( is_string ($ validator ) && isset ($ this ->_validators ['validator ' ]) ) {
233
+ array_unshift ($ copy , $ data [$ name ]);// 压入当前属性值
230
234
231
- $ callback = $ this ->_validators ['validator ' ];
235
+ // if it's a closure
236
+ if ( is_callable ($ validator ) && $ validator instanceof \Closure) {
237
+ $ callback = $ validator ;
238
+ $ validator = 'callback ' ;
232
239
233
- // if it is a custom method of the subclass .
234
- } elseif ( is_string ($ validator ) && method_exists ($ this , $ validator ) ) {
240
+ // if it is a custom add callback in the property {@see $_validators} .
241
+ } elseif ( is_string ($ validator ) && isset ($ this -> _validators [ ' validator ' ] ) ) {
235
242
236
- $ callback = [ $ this , $ validator ];
243
+ $ callback = $ this -> _validators [ ' validator ' ];
237
244
238
- // it's a method of the class 'ValidatorList'
239
- } elseif ( is_string ($ validator ) && is_callable ([ValidatorList::class , $ validator] ) ) {
245
+ // if it is a custom method of the subclass.
246
+ } elseif ( is_string ($ validator ) && method_exists ( $ this , $ validator ) ) {
240
247
241
- $ callback = [ ValidatorList::class, $ validator ];
242
- } else {
243
- throw new \InvalidArgumentException ("validator [ $ validator] don't exists! " );
244
- }
248
+ $ callback = [ $ this , $ validator ];
245
249
246
- $ result = call_user_func_array ($ callback , $ copy );
250
+ // it's a method of the class 'ValidatorList'
251
+ } elseif ( is_string ($ validator ) && is_callable ([ValidatorList::class, $ validator ]) ) {
252
+
253
+ $ callback = [ ValidatorList::class, $ validator ];
254
+ } else {
255
+ throw new \InvalidArgumentException ("validator [ $ validator] don't exists! " );
247
256
}
248
257
258
+ $ result = call_user_func_array ($ callback , $ copy );
259
+
249
260
return [$ result ,$ validator ];
250
261
}
251
262
@@ -288,6 +299,12 @@ protected function collectRules()
288
299
289
300
// 循环规则, 搜集当前场景的规则
290
301
foreach ($ this ->getRules () as $ rule ) {
302
+
303
+ // check validator
304
+ if ( !is_string ($ rule [1 ]) && !($ rule [1 ] instanceof \Closure) ) {
305
+ throw new \InvalidArgumentException ("validator setting error! " );
306
+ }
307
+
291
308
if ( empty ($ rule ['scene ' ]) ) {
292
309
$ availableRules [] = $ rule ;
293
310
} else {
0 commit comments