0 follower

Final Class Yiisoft\Validator\Rule\AnyRule

InheritanceYiisoft\Validator\Rule\AnyRule
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 passed. The opposite of Yiisoft\Validator\Rule\StopOnError.

An example of usage:

$rule = new AnyRule([
     new IntegerType(), // Let's say the validation passed here.
     new FloatType(), // Then this rule will be skipped.
]);

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

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

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Rule\AnyRule
afterInitAttribute() Yiisoft\Validator\Rule\AnyRule
getHandler() Yiisoft\Validator\Rule\AnyRule
getMessage() Gets error message used when validation fails because none of the inner $rules has passed the validation. Yiisoft\Validator\Rule\AnyRule
getName() Yiisoft\Validator\Rule\AnyRule
getOptions() Yiisoft\Validator\Rule\AnyRule
getRules() Gets a set of rules for running the validation. Yiisoft\Validator\Rule\AnyRule
getSkipOnEmpty() A getter for $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
getWhen() A getter for $when property. Yiisoft\Validator\Rule\Trait\WhenTrait
shouldSkipOnError() A getter for $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
skipOnEmpty() An immutable setter to change $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
skipOnError() An immutable setter to change $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
when() An immutable setter to change $when property. Yiisoft\Validator\Rule\Trait\WhenTrait

Method Details

Hide inherited methods

__construct() public method

public __construct( iterable $rules, string $message 'At least one of the inner rules must pass the validation.', boolean|callable|null $skipOnEmpty null, boolean $skipOnError false, Closure|null $when null ): mixed
$rules iterable

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

$message string

Error message used when validation fails because none of the inner $rules has passed the validation.

$skipOnEmpty boolean|callable|null

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

$skipOnError boolean

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

$when Closure|null

A callable to define a condition for applying this Any rule with all defined $rules. See Yiisoft\Validator\WhenInterface.

                public function __construct(
    iterable $rules,
    private string $message = 'At least one of the inner rules must pass the validation.',
    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 afterInitAttribute( object $object ): void
$object object

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

            
getHandler() public method

public getHandler( ): string

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

            
getMessage() public method

Gets error message used when validation fails because none of the inner $rules has passed the validation.

public getMessage( ): string
return string

Error message / template.

                public function getMessage(): string
{
    return $this->message;
}

            
getName() public method

public getName( ): string

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

            
getOptions() public method

public getOptions( ): array

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

            
getRules() public method

Gets a set of rules for running the validation.

public getRules( ): iterable
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 getSkipOnEmpty( ): boolean|callable|null
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 getWhen( ): Closure|null
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 shouldSkipOnError( ): boolean
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 skipOnEmpty( boolean|callable|null $value ): $this
$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 skipOnError( boolean $value ): $this
$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 when( Closure|null $value ): $this
$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;
}