@@ -131,7 +131,7 @@ if ($result->isValid() === false) {
131131#### Skipping validation on error
132132
133133By default, if an error occurred during validation of an attribute, further rules for this attribute are processed.
134- To change this behavior use ` skipOnError: true ` when configuring rules:
134+ To change this behavior, use ` skipOnError: true ` when configuring rules:
135135
136136``` php
137137use Yiisoft\Validator\Rule\Number;
@@ -142,12 +142,80 @@ new Number(asInteger: true, max: 100, skipOnError: true)
142142#### Skipping empty values
143143
144144By default, empty values are validated. That is undesirable if you need to allow not specifying a field.
145- To change this behavior use ` skipOnEmpty: true ` :
145+ To change this behavior, use ` skipOnEmpty: true ` :
146146
147147``` php
148148use Yiisoft\Validator\Rule\Number;
149149
150- new Number(asInteger: true, max: 100, skipOnEmpty: true)
150+ new Number(asInteger: true, max: 100, skipOnEmpty: true);
151+ ```
152+
153+ What exactly to consider to be empty is vague and can vary depending on a scope of usage.
154+
155+ ` skipOnEmpty ` is more like a shortcut, ` skipOnEmptyCallback ` argument checks if a given value is empty:
156+
157+ - If ` skipOnEmpty ` is ` false ` , ` Yiisoft\Validator\SkipOnEmptyCallback\SkipNone ` is used automatically for
158+ ` skipOnEmptyCallback ` - every value is considered non-empty and validated without skipping (default).
159+ - If ` skipOnEmpty ` is ` true ` , ` Yiisoft\Validator\SkipOnEmptyCallback\SkipOnEmpty ` is used automatically for
160+ ` skipOnEmptyCallback ` - only non-empty values (not ` null ` , ` [] ` , or ` '' ` ) are validated.
161+ - If ` skipOnEmptyCallback ` is set, it takes precedence to determine emptiness.
162+
163+ Using first option is usually good for HTML forms. The second one is more suitable for APIs.
164+
165+ The empty values can be also limited to ` null ` only:
166+
167+ ``` php
168+ use Yiisoft\Validator\Rule\Number;
169+ use Yiisoft\Validator\SkipOnEmptyCallback\SkipOnNull;
170+
171+ new Number(asInteger: true, max: 100, skipOnEmptyCallback: new SkipOnNull());
172+ ```
173+
174+ Note that in this case ` skipOnEmpty ` will be automatically set to ` true ` , so there is no need to do it manually.
175+
176+ For even more customization you can use your own class implementing ` __invoke() ` magic method:
177+
178+ ``` php
179+ use Yiisoft\Validator\Rule\Number;
180+
181+ final class SkipOnZero
182+ {
183+ public function __invoke($value): bool
184+ {
185+ return $value === 0;
186+ }
187+ }
188+
189+ new Number(asInteger: true, max: 100, skipOnEmptyCallback: new SkipOnZero());
190+ ```
191+
192+ or just a callable:
193+
194+ ``` php
195+ use Yiisoft\Validator\Rule\Number;
196+
197+ new Number(
198+ asInteger: true,
199+ max: 100,
200+ skipOnEmptyCallback: static function (mixed $value): bool {
201+ return $value === 0;
202+ }
203+ );
204+ ```
205+
206+ For multiple rules this can be also set more conveniently at validator level:
207+
208+ ``` php
209+ use Yiisoft\Validator\SimpleRuleHandlerContainer;
210+ use Yiisoft\Validator\Validator;
211+
212+ $validator = new Validator(new SimpleRuleHandlerContainer(), skipOnEmpty: true);
213+ $validator = new Validator(
214+ new SimpleRuleHandlerContainer(),
215+ skipOnEmptyCallback: static function (mixed $value): bool {
216+ return $value === 0;
217+ }
218+ );
151219```
152220
153221#### Nested and related data
0 commit comments