@@ -37,8 +37,11 @@ trait ValidationTrait
3737 /** @var \Closure after validate handler */
3838 private $ _afterHandler ;
3939
40+ /** @var array Used rules at current scene */
41+ protected $ _usedRules = [];
42+
4043 /**
41- * current scenario name
44+ * setting current scenario name
4245 * 当前验证的场景 -- 如果需要让规则列表在多个类似情形下使用
4346 * (
4447 * e.g: 在MVC框架中,
@@ -49,8 +52,12 @@ trait ValidationTrait
4952 */
5053 protected $ scene = '' ;
5154
52- /** @var array Used rules at current scene */
53- protected $ _usedRules = [];
55+ /**
56+ * Whether to skip when empty(When not required)
57+ * default is TRUE, need you manual add 'required'.
58+ * @var bool
59+ */
60+ private $ _skipOnEmpty = true ;
5461
5562 /**
5663 * @return array
@@ -87,8 +94,8 @@ public function messages()
8794 }
8895
8996 /**
90- * 当前场景需要收集的字段
91- * @todo un-complete
97+ * The field that the current scene needs to collect
98+ * @return array
9299 */
93100 public function scenarios (): array
94101 {
@@ -163,6 +170,10 @@ public function validate(array $onlyChecked = [], bool $stopOnError = null)
163170 $ cb ($ this );
164171 }
165172
173+ if (!$ onlyChecked ) {
174+ $ onlyChecked = $ this ->getSceneFields ();
175+ }
176+
166177 foreach ($ this ->collectRules () as $ fields => $ rule ) {
167178 $ fields = \is_string ($ fields ) ? Helper::explode ($ fields ) : (array )$ fields ;
168179 $ validator = \array_shift ($ rule );
@@ -180,7 +191,7 @@ public function validate(array $onlyChecked = [], bool $stopOnError = null)
180191 }
181192
182193 // Whether to skip when empty(When not required). ref yii2
183- $ skipOnEmpty = $ rule ['skipOnEmpty ' ] ?? true ;
194+ $ skipOnEmpty = $ rule ['skipOnEmpty ' ] ?? $ this -> _skipOnEmpty ;
184195 $ filters = $ rule ['filter ' ] ?? null ; // filter
185196 $ defMsg = $ rule ['msg ' ] ?? null ; // Custom error message
186197 $ defValue = $ rule ['default ' ] ?? null ;// Allow default
@@ -474,6 +485,19 @@ protected function prepareRule(array &$rule)
474485 * getter/setter/helper
475486 ******************************************************************************/
476487
488+ /**
489+ * get fields for the scene.
490+ * @return array
491+ */
492+ public function getSceneFields (): array
493+ {
494+ if ($ this ->scene && $ conf = $ this ->scenarios ()) {
495+ return $ conf [$ this ->scene ] ?? [];
496+ }
497+
498+ return [];
499+ }
500+
477501 /**
478502 * @param string $path 'users.*.id' 'goods.*' 'foo.bar.*.id'
479503 * @param null|mixed $default
@@ -570,7 +594,6 @@ public function getScene(): string
570594 public function atScene (string $ scene ): self
571595 {
572596 $ this ->scene = \trim ($ scene );
573-
574597 return $ this ;
575598 }
576599
@@ -594,6 +617,16 @@ public function onScene(string $scene)
594617 return $ this ->atScene ($ scene );
595618 }
596619
620+ /**
621+ * @param bool $_skipOnEmpty
622+ * @return static
623+ */
624+ public function setSkipOnEmpty (bool $ _skipOnEmpty )
625+ {
626+ $ this ->_skipOnEmpty = $ _skipOnEmpty ;
627+ return $ this ;
628+ }
629+
597630 /**
598631 * Get all items in collection
599632 * @return array The collection's source data
0 commit comments