Class Yiisoft\Validator\Rule\Composite
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
| Property | Type | Description | Defined By |
|---|---|---|---|
| $rules | iterable | A set of normalized rules that needs to be grouped. | Yiisoft\Validator\Rule\Composite |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Validator\Rule\Composite | |
| afterInitAttribute() | Yiisoft\Validator\Rule\Composite | |
| getHandler() | Yiisoft\Validator\Rule\Composite | |
| getName() | Yiisoft\Validator\Rule\Composite | |
| getOptions() | Yiisoft\Validator\Rule\Composite | |
| getRules() | Gets a set of normalized rules that needs to be grouped. | Yiisoft\Validator\Rule\Composite |
| 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 |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| dumpRulesAsArray() | Dumps grouped {@see $rules} to array. | Yiisoft\Validator\Rule\Composite |
Property Details
Method Details
| 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;
}
| 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);
}
}
}
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());
}
| public string getHandler ( ) |
final public function getHandler(): string
{
return CompositeHandler::class;
}
| public array getOptions ( ) |
#[ArrayShape([
'skipOnEmpty' => 'bool',
'skipOnError' => 'bool',
'rules' => 'array',
])]
public function getOptions(): array
{
return [
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
'skipOnError' => $this->skipOnError,
'rules' => $this->dumpRulesAsArray(),
];
}
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;
}
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;
}
Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()
A getter for $when property.
| public Closure|null getWhen ( ) | ||
| return | Closure|null |
Current value:
|
|---|---|---|
public function getWhen(): ?Closure
{
return $this->when;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::shouldSkipOnError()
A getter for $skipOnError property.
| public boolean shouldSkipOnError ( ) | ||
| return | boolean |
Current value. |
|---|---|---|
public function shouldSkipOnError(): bool
{
return $this->skipOnError;
}
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;
}
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. |
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function skipOnError(bool $value): static
{
$new = clone $this;
$new->skipOnError = $value;
return $new;
}
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:
|
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function when(?Closure $value): static
{
$new = clone $this;
$new->when = $value;
return $new;
}
Signup or Login in order to comment.