@@ -52,7 +52,7 @@ trait ValidationTrait
5252 private $ _hasErrorStop = false ;
5353
5454 /**
55- * all rules
55+ * the rules is by setRules()
5656 * @var array
5757 */
5858 private $ _rules = [];
@@ -64,6 +64,7 @@ trait ValidationTrait
6464 private $ _availableRules = [];
6565
6666 /**
67+ * custom append's validator by addValidator()
6768 * @var array
6869 */
6970 private $ _validators = [];
@@ -79,10 +80,10 @@ trait ValidationTrait
7980 */
8081 private $ _hasValidated = false ;
8182
82- private $ _position = [
83- 'attr ' => 0 ,
84- 'validator ' => 1 ,
85- ];
83+ // private $_position = [
84+ // 'attr' => 0,
85+ // 'validator' => 1,
86+ // ];
8687
8788 /**
8889 * @return array
@@ -152,7 +153,7 @@ public function validate(array $onlyChecked = [], $hasErrorStop=false)
152153 return $ this ;
153154 }
154155
155- $ this ->clearErrors ( )->beforeValidate ();
156+ $ this ->resetRuntimeData ( true )->beforeValidate ();
156157 $ this ->hasErrorStop ($ hasErrorStop );
157158
158159 $ data = $ this ->data ;
@@ -161,7 +162,7 @@ public function validate(array $onlyChecked = [], $hasErrorStop=false)
161162 foreach ($ this ->collectRules () as $ rule ) {
162163 // 要检查的属性(字段)名称集
163164 $ attrs = array_shift ($ rule );
164- $ attrs = is_string ($ attrs ) ? array_filter ( explode (', ' , $ attrs ), ' trim ' ) : (array )$ attrs ;
165+ $ attrs = is_string ($ attrs ) ? array_map ( ' trim ' , explode (', ' , $ attrs )) : (array )$ attrs ;
165166
166167 // 要使用的验证器(a string or a Closure)
167168 $ validator = array_shift ($ rule );
@@ -273,19 +274,19 @@ protected function doValidate($data, $attr, $validator, $args)
273274 $ args [] = $ data ;
274275
275276 } elseif ( is_string ($ validator ) ) {
276-
277+
277278 // if $validator is a custom add callback in the property {@see $_validators}.
278279 if ( isset ($ this ->_validators ['validator ' ]) ) {
279280 $ callback = $ this ->_validators ['validator ' ];
280-
281+
281282 // if $validator is a custom method of the subclass.
282283 } elseif ( is_string ($ validator ) && method_exists ($ this , $ validator ) ) {
283-
284+
284285 $ callback = [ $ this , $ validator ];
285-
286+
286287 // $validator is a method of the class 'ValidatorList'
287288 } elseif ( is_string ($ validator ) && is_callable ([ValidatorList::class, $ validator ]) ) {
288-
289+
289290 $ callback = [ ValidatorList::class, $ validator ];
290291 } else {
291292 throw new \InvalidArgumentException ("validator [ $ validator] don't exists! " );
@@ -301,6 +302,21 @@ protected function doValidate($data, $attr, $validator, $args)
301302
302303 public function afterValidate (){}
303304
305+ /**
306+ * @param bool|false $clearErrors
307+ * @return $this
308+ */
309+ protected function resetRuntimeData ($ clearErrors = false )
310+ {
311+ $ this ->_safeData = $ this ->_availableRules = [];
312+
313+ if ( $ clearErrors ) {
314+ $ this ->clearErrors ();
315+ }
316+
317+ return $ this ;
318+ }
319+
304320 /**
305321 * add a custom validator
306322 *
@@ -349,7 +365,7 @@ protected function collectRules()
349365 $ availableRules [] = $ rule ;
350366 } else {
351367 $ ruleScene = $ rule ['on ' ];
352- $ ruleScene = is_string ($ ruleScene ) ? array_filter ( explode (', ' , $ ruleScene ), ' trim ' ) : (array )$ ruleScene ;
368+ $ ruleScene = is_string ($ ruleScene ) ? array_map ( ' trim ' , explode (', ' , $ ruleScene )) : (array )$ ruleScene ;
353369
354370 if ( in_array ($ scene ,$ ruleScene ) ) {
355371 unset($ rule ['on ' ]);
@@ -523,7 +539,7 @@ public function getAttrTrans()
523539 */
524540 public function setAttrTrans (array $ attrTrans )
525541 {
526- $ this ->_attrTrans = array_merge ( $ this -> _attrTrans , $ attrTrans) ;
542+ $ this ->_attrTrans = $ attrTrans ;
527543
528544 return $ this ;
529545 }
@@ -541,11 +557,7 @@ public function hasRule()
541557 */
542558 public function getRules ()
543559 {
544- if ( !$ this ->_rules ) {
545- $ this ->_rules = $ this ->rules ();
546- }
547-
548- return $ this ->_rules ;
560+ return array_merge ($ this ->rules (), $ this ->_rules );
549561 }
550562
551563 /**
@@ -559,6 +571,14 @@ public function setRules(array $rules)
559571 return $ this ;
560572 }
561573
574+ /**
575+ * @return array
576+ */
577+ public function getAvailableRules ()
578+ {
579+ return $ this ->_availableRules ;
580+ }
581+
562582 /**
563583 * @return string
564584 */
@@ -590,9 +610,7 @@ public function all()
590610
591611 /**
592612 * Does this collection have a given key?
593- *
594613 * @param string $key The data key
595- *
596614 * @return bool
597615 */
598616 public function has ($ key )
@@ -602,24 +620,21 @@ public function has($key)
602620
603621 /**
604622 * Set data item
605- *
606623 * @param string $key The data key
607624 * @param mixed $value The data value
608625 * @return $this
609626 */
610- public function set ($ key , $ value )
611- {
612- $ this ->data [$ key ] = $ value ;
613-
614- return $ this ;
615- }
627+ // public function set($key, $value)
628+ // {
629+ // $this->data[$key] = $value;
630+ //
631+ // return $this;
632+ // }
616633
617634 /**
618635 * Get data item for key
619- *
620636 * @param string $key The data key
621637 * @param mixed $default The default value to return if data key does not exist
622- *
623638 * @return mixed The key's value, or the default value
624639 */
625640 public function get ($ key , $ default = null )
@@ -649,4 +664,12 @@ public function getSafeData()
649664 {
650665 return $ this ->_safeData ;
651666 }
667+
668+ /**
669+ * @return array
670+ */
671+ public function getSafeKeys ()
672+ {
673+ return array_keys ($ this ->_safeData );
674+ }
652675}
0 commit comments