@@ -71,6 +71,11 @@ trait ValidationTrait
7171 */
7272 private $ _hasValidated = false ;
7373
74+ private $ _position = [
75+ 'attr ' => 0 ,
76+ 'validator ' => 1 ,
77+ ];
78+
7479 /**
7580 * @return array
7681 */
@@ -215,37 +220,43 @@ public function validate(array $onlyChecked = [], $hasErrorStop=false)
215220 */
216221 protected function doValidate ($ data , $ name , $ validator , $ copy )
217222 {
218- $ result = ValidatorList::required ($ data , $ name );
223+ if ( !isset ($ data [$ name ]) ) {
224+ return [false , $ validator instanceof \Closure ? 'callback ' : $ validator ];
225+ }
219226
220- if ($ result && $ validator !== 'required ' ) {
221- array_unshift ( $ copy , $ data [ $ name]); // 压入当前属性值
227+ if ( $ validator === 'required ' ) {
228+ $ result = ValidatorList:: required ( $ data , $ name);
222229
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+ }
227232
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 ]);// 压入当前属性值
230234
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 ' ;
232239
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 ' ] ) ) {
235242
236- $ callback = [ $ this , $ validator ];
243+ $ callback = $ this -> _validators [ ' validator ' ];
237244
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 ) ) {
240247
241- $ callback = [ ValidatorList::class, $ validator ];
242- } else {
243- throw new \InvalidArgumentException ("validator [ $ validator] don't exists! " );
244- }
248+ $ callback = [ $ this , $ validator ];
245249
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! " );
247256 }
248257
258+ $ result = call_user_func_array ($ callback , $ copy );
259+
249260 return [$ result ,$ validator ];
250261 }
251262
@@ -288,6 +299,12 @@ protected function collectRules()
288299
289300 // 循环规则, 搜集当前场景的规则
290301 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+
291308 if ( empty ($ rule ['scene ' ]) ) {
292309 $ availableRules [] = $ rule ;
293310 } else {
0 commit comments