0 follower

Final Class Yiisoft\Validator\Rule\StopOnError

InheritanceYiisoft\Validator\Rule\StopOnError
ImplementsYiisoft\Validator\AfterInitAttributeEventInterface, Yiisoft\Validator\DumpedRuleInterface, Yiisoft\Validator\SkipOnEmptyInterface, Yiisoft\Validator\SkipOnErrorInterface, Yiisoft\Validator\WhenInterface
Uses TraitsYiisoft\Validator\Rule\Trait\SkipOnEmptyTrait, Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait, Yiisoft\Validator\Rule\Trait\WhenTrait

Applies to a set of rules, runs validation for each one of them in the order they are defined and stops at the rule where validation failed.

An example of usage:

$rule = new StopOnError([
     new Required(), // Let's say there is an error.
     new Length(min: 3), // Then this rule will be skipped.
     new MyCustomRule(), // This rule will be skipped too.
]);

When using with other rules, conditional validation options, such as {@see \Yiisoft\Validator\Rule\StopOnError::$skipOnError} will be applied to the whole group of {@see \Yiisoft\Validator\Rule\StopOnError::$rules}.

Not to be confused with skipping each rule individually, there is a separate functionality for that, see {@see \Yiisoft\Validator\SkipOnErrorInterface}.

There is a similar rule that stops on successful validation - {@see \Yiisoft\Validator\Rule\AnyRule}.

See also Yiisoft\Validator\Rule\StopOnErrorHandler Corresponding handler performing the actual validation.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( iterable $rules, boolean|callable|null $skipOnEmpty null, boolean $skipOnError false, Closure|null $when null )
$rules iterable

A set of rules for running the validation. They will be normalized during initialization using {@see \Yiisoft\Validator\Helper\RulesNormalizer}.

$skipOnEmpty boolean|callable|null

Whether to skip this StopOnError rule with all defined {@see $rules} if the validated value is empty / not passed. See {@see \Yiisoft\Validator\SkipOnEmptyInterface}.

$skipOnError boolean

Whether to skip this StopOnError rule with all defined {@see $rules} if any of the previous rules gave an error. See {@see \Yiisoft\Validator\SkipOnErrorInterface}.

$when Closure|null

A callable to define a condition for applying this StopOnError rule with all defined {@see $rules}. See {@see \Yiisoft\Validator\WhenInterface}.

                public function __construct(
    iterable $rules,
    bool|callable|null $skipOnEmpty = null,
    private bool $skipOnError = false,
    private ?Closure $when = null,
) {
    $this->rules = RulesNormalizer::normalizeList($rules);
    $this->skipOnEmpty = $skipOnEmpty;
}

            
afterInitAttribute() public method

public void afterInitAttribute ( object $object )
$object object

                public function afterInitAttribute(object $object): void
{
    foreach ($this->rules as $rule) {
        if ($rule instanceof AfterInitAttributeEventInterface) {
            $rule->afterInitAttribute($object);
        }
    }
}

            
getHandler() public method

public string getHandler ( )

                public function getHandler(): string
{
    return StopOnErrorHandler::class;
}

            
getName() public method

public string getName ( )

                public function getName(): string
{
    return self::class;
}

            
getOptions() public method

public array getOptions ( )

                #[ArrayShape([
    'skipOnEmpty' => 'bool',
    'skipOnError' => 'bool',
    'rules' => 'array|null',
])]
public function getOptions(): array
{
    return [
        'skipOnEmpty' => $this->getSkipOnEmptyOption(),
        'skipOnError' => $this->skipOnError,
        'rules' => $this->dumpRulesAsArray(),
    ];
}

            
getRules() public method

Gets a set of rules for running the validation.

public iterable getRules ( )
return iterable

A set of rules.

                public function getRules(): iterable
{
    return $this->rules;
}

            
getSkipOnEmpty() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::getSkipOnEmpty()

A getter for $skipOnEmpty property.

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

A current raw (non-normalized) value.

                public function getSkipOnEmpty(): bool|callable|null
{
    return $this->skipOnEmpty;
}

            
getWhen() public method

Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()

A getter for $when property.

public Closure|null getWhen ( )
return Closure|null

Current value:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.

                public function getWhen(): ?Closure
{
    return $this->when;
}

            
shouldSkipOnError() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::shouldSkipOnError()

A getter for $skipOnError property.

public boolean shouldSkipOnError ( )
return boolean

Current value. true means to skip the current rule when the previous one errored and false - do not skip.

                public function shouldSkipOnError(): bool
{
    return $this->skipOnError;
}

            
skipOnEmpty() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::skipOnEmpty()

An immutable setter to change $skipOnEmpty property.

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

A new value.

return $this

The new instance with a changed value.

                public function skipOnEmpty(bool|callable|null $value): static
{
    $new = clone $this;
    $new->skipOnEmpty = $value;
    return $new;
}

            
skipOnError() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::skipOnError()

An immutable setter to change $skipOnError property.

public $this skipOnError ( boolean $value )
$value boolean

A new value. true means to skip the current rule when the previous one errored and false - do not skip.

return $this

The new instance with a changed value.

                public function skipOnError(bool $value): static
{
    $new = clone $this;
    $new->skipOnError = $value;
    return $new;
}

            
when() public method

Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::when()

An immutable setter to change $when property.

public $this when ( Closure|null $value )
$value Closure|null

A new value:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.
return $this

The new instance with a changed value.

                public function when(?Closure $value): static
{
    $new = clone $this;
    $new->when = $value;
    return $new;
}