0 follower

Interface Yiisoft\Validator\SkipOnEmptyInterface

Implemented byYiisoft\Validator\Rule\AbstractCompare, Yiisoft\Validator\Rule\AbstractNumber, Yiisoft\Validator\Rule\AnyRule, Yiisoft\Validator\Rule\BooleanValue, Yiisoft\Validator\Rule\Callback, Yiisoft\Validator\Rule\Compare, Yiisoft\Validator\Rule\Composite, Yiisoft\Validator\Rule\Count, Yiisoft\Validator\Rule\Date\BaseDate, Yiisoft\Validator\Rule\Date\Date, Yiisoft\Validator\Rule\Date\DateTime, Yiisoft\Validator\Rule\Date\Time, Yiisoft\Validator\Rule\Each, Yiisoft\Validator\Rule\Email, Yiisoft\Validator\Rule\Equal, Yiisoft\Validator\Rule\FilledAtLeast, Yiisoft\Validator\Rule\FilledOnlyOneOf, Yiisoft\Validator\Rule\GreaterThan, Yiisoft\Validator\Rule\GreaterThanOrEqual, Yiisoft\Validator\Rule\Image\Image, Yiisoft\Validator\Rule\In, Yiisoft\Validator\Rule\InEnum, Yiisoft\Validator\Rule\Integer, Yiisoft\Validator\Rule\Ip, Yiisoft\Validator\Rule\Json, Yiisoft\Validator\Rule\Length, Yiisoft\Validator\Rule\LessThan, Yiisoft\Validator\Rule\LessThanOrEqual, Yiisoft\Validator\Rule\Nested, Yiisoft\Validator\Rule\NotEqual, Yiisoft\Validator\Rule\Number, Yiisoft\Validator\Rule\Regex, Yiisoft\Validator\Rule\StopOnError, Yiisoft\Validator\Rule\StringValue, Yiisoft\Validator\Rule\Subset, Yiisoft\Validator\Rule\TrueValue, Yiisoft\Validator\Rule\Type\BooleanType, Yiisoft\Validator\Rule\Type\FloatType, Yiisoft\Validator\Rule\Type\IntegerType, Yiisoft\Validator\Rule\Type\StringType, Yiisoft\Validator\Rule\UniqueIterable, Yiisoft\Validator\Rule\Url, Yiisoft\Validator\Rule\Uuid

An optional interface for rules to implement. It allows skipping validation for a rule when the validated value is "empty".

The package ships with {@see \Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait} which already implements that interface. All you have to do is include it in the rule class along with the interface.

Psalm Types

Name Value
SkipOnEmptyCallable callable
SkipOnEmptyValue \Yiisoft\Validator\SkipOnEmptyCallable|boolean|null

Public Methods

Hide inherited methods

Method Description Defined By
getSkipOnEmpty() Returns current "skip on empty" value. Yiisoft\Validator\SkipOnEmptyInterface
skipOnEmpty() Changes current "skip on empty" value. Must be immutable. Yiisoft\Validator\SkipOnEmptyInterface

Method Details

Hide inherited methods

getSkipOnEmpty() public abstract method

Returns current "skip on empty" value.

During pre-validation phase it will be normalized to an "empty condition" - a callable identifying when and which values exactly must be considered as empty for corresponding rules to be skipped or not skipped at all.

public abstract boolean|callable|null getSkipOnEmpty ( )
return boolean|callable|null

A raw non-normalized value:

  • null - a {@see \Yiisoft\Validator\Validator::$defaultSkipOnEmptyCondition} is used. If it's null there too, it's equivalent to false (see below). false - never skip a rule (the validated value is always considered as not empty). Matching condition - {@see \Yiisoft\Validator\EmptyCondition\NeverEmpty}.
  • true - skip a rule when the validated value is empty: either not passed at all, null, an empty string (not trimmed by default) or an empty iterable. Matching condition - {@see \Yiisoft\Validator\EmptyCondition\WhenEmpty}.
  • callable - skip a rule when evaluated to true.

Examples of custom callables with built-in condition:

  • new WhenNull() - less used built-in condition ({@see \Yiisoft\Validator\EmptyCondition\WhenNull}). Skip a rule only when the validated value is null.
  • new WhenEmpty(trimString: true) - built-in condition with custom arguments ({@see \Yiisoft\Validator\EmptyCondition\WhenEmpty}).

A custom callable for skipping only when a value is zero:

static function (mixed $value, bool $isPropertyMissing): bool {
{
    return $value === 0;
}

An equivalent class implementing __invoke() magic method:

final class SkipOnZero
{
    public function __invoke(mixed $value, bool $isPropertyMissing): bool
    {
        return $value === 0;
    }
}

                public function getSkipOnEmpty(): bool|callable|null;

            
skipOnEmpty() public abstract method

Changes current "skip on empty" value. Must be immutable.

public abstract $this skipOnEmpty ( boolean|callable|null $value )
$value boolean|callable|null

A new value.

return $this

The new instance of a rule with a changed value.

                public function skipOnEmpty(bool|callable|null $value): static;