0 follower

Class Yiisoft\Validator\Rule\Composite

InheritanceYiisoft\Validator\Rule\Composite
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

Allows to group multiple rules for validation. It's helpful when skipOnEmpty, skipOnError or when options are the same for every rule in the set.

For example, with the same when closure, without using composite it's specified explicitly for every rule:

$when = static function ($value, ValidationContext $context): bool {
    return $context->getDataSet()->getPropertyValue('country') === Country::USA;
};
$rules = [
    new Required(when: $when),
    new Length(min: 1, max: 50, skipOnEmpty: true, when: $when),
];

When using composite, specifying it only once will be enough:

$rule = new Composite([
    new Required(),
    new Length(min: 1, max: 50, skipOnEmpty: true),
    when: static function ($value, ValidationContext $context): bool {
        return $context->getDataSet()->getPropertyValue('country') === Country::USA;
    },
]);

Another use case is reusing this rule group across different places. It's possible by creating own extended class and setting the properties in the constructor:

class MyComposite extends Composite
{
    public function __construct()
    {
        $this->rules = [
            new Required(),
            new Length(min: 1, max: 50, skipOnEmpty: true),
        ];
        $this->when = static function ($value, ValidationContext $context): bool {
            return $context->getDataSet()->getPropertyValue('country') === Country::USA;
        };
    }
};

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

Protected Properties

Hide inherited properties

Property Type Description Defined By
$rules iterable A set of normalized rules that needs to be grouped. Yiisoft\Validator\Rule\Composite

Protected Methods

Hide inherited methods

Method Description Defined By
dumpRulesAsArray() Dumps grouped {@see $rules} to array. Yiisoft\Validator\Rule\Composite

Property Details

Hide inherited properties

$rules protected property

A set of normalized rules that needs to be grouped.

protected iterable $rules = []

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 that needs to be grouped. They will be normalized using {@see \Yiisoft\Validator\Helper\RulesNormalizer}.

$skipOnEmpty boolean|callable|null

Whether to skip this rule group if the validated value is empty / not passed. See {@see \Yiisoft\Validator\SkipOnEmptyInterface}.

$skipOnError boolean

Whether to skip this rule group 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 rule group. See {@see \Yiisoft\Validator\WhenInterface}.

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

            
afterInitAttribute() public method

public void afterInitAttribute ( object $object )
$object object

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

            
dumpRulesAsArray() protected method

Dumps grouped {@see $rules} to array.

protected array dumpRulesAsArray ( )
return array

The array of rules with their options.

                final protected function dumpRulesAsArray(): array
{
    return RulesDumper::asArray($this->getRules());
}

            
getHandler() public method

public string getHandler ( )

                final public function getHandler(): string
{
    return CompositeHandler::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',
])]
public function getOptions(): array
{
    return [
        'skipOnEmpty' => $this->getSkipOnEmptyOption(),
        'skipOnError' => $this->skipOnError,
        'rules' => $this->dumpRulesAsArray(),
    ];
}

            
getRules() public method

Gets a set of normalized rules that needs to be grouped.

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;
}