0 follower

Final Class Yiisoft\Validator\Rule\Subset

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

A variation of {@see In} rule allowing to use the set of values instead of single value as an input for checking if it's a subset of the set provided in {@see Subset::$values}.

The order of items in the validated set is not important.

Nested arrays are supported in both {@see $values} argument and in the validated value (the order of values in lists must match, the order of keys in associative arrays is not important).

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

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Rule\Subset
getHandler() Yiisoft\Validator\Rule\Subset
getIncorrectInputMessage() Gets error message used when validation fails because the type of validated value is incorrect. Yiisoft\Validator\Rule\Subset
getMessage() Gets error message used when validation fails because the validated value is not a subset of the set provided in {@see $values}. Yiisoft\Validator\Rule\Subset
getName() Yiisoft\Validator\Rule\Subset
getOptions() Yiisoft\Validator\Rule\Subset
getSkipOnEmpty() A getter for $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
getValues() Gets a set of values to check against. Yiisoft\Validator\Rule\Subset
getWhen() A getter for $when property. Yiisoft\Validator\Rule\Trait\WhenTrait
isStrict() Whether the comparison for each value in the set is strict. Yiisoft\Validator\Rule\Subset
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 mixed __construct ( iterable $values, boolean $strict false, string $incorrectInputMessage '{Property} must be iterable. {type} given.', string $message '{Property} is not a subset of acceptable values.', boolean|callable|null $skipOnEmpty null, boolean $skipOnError false, Closure|null $when null )
$values iterable

A set of values to check against. Nested arrays are supported too (the order of values in lists must match, the order of keys in associative arrays is not important).

$strict boolean

Whether the comparison for each value in the set is strict:

Defaults to false meaning non-strict mode is used.

$incorrectInputMessage string

Error message used when validation fails because the validated value is not an iterable.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {type}: the type of the value being validated.
$message string

Error message used when validation fails because the validated value is not a subset of the set provided in {@see $values}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
$skipOnEmpty boolean|callable|null

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

$skipOnError boolean

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

                public function __construct(
    private iterable $values,
    private bool $strict = false,
    private string $incorrectInputMessage = '{Property} must be iterable. {type} given.',
    private string $message = '{Property} is not a subset of acceptable values.',
    bool|callable|null $skipOnEmpty = null,
    private bool $skipOnError = false,
    private ?Closure $when = null,
) {
    $this->skipOnEmpty = $skipOnEmpty;
}

            
getHandler() public method

public string getHandler ( )

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

            
getIncorrectInputMessage() public method

Gets error message used when validation fails because the type of validated value is incorrect.

public string getIncorrectInputMessage ( )
return string

Error message / template.

                public function getIncorrectInputMessage(): string
{
    return $this->incorrectInputMessage;
}

            
getMessage() public method

Gets error message used when validation fails because the validated value is not a subset of the set provided in {@see $values}.

public string getMessage ( )
return string

Error message / template.

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

            
getName() public method

public string getName ( )

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

            
getOptions() public method

public array getOptions ( )

                public function getOptions(): array
{
    return [
        'values' => $this->values,
        'strict' => $this->strict,
        'incorrectInputMessage' => [
            'template' => $this->incorrectInputMessage,
            'parameters' => [],
        ],
        'message' => [
            'template' => $this->message,
            'parameters' => [],
        ],
        'skipOnEmpty' => $this->getSkipOnEmptyOption(),
        'skipOnError' => $this->skipOnError,
    ];
}

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

            
getValues() public method

Gets a set of values to check against.

public iterable getValues ( )
return iterable

A set of values.

                public function getValues(): iterable
{
    return $this->values;
}

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

            
isStrict() public method

Whether the comparison for each value in the set is strict.

public boolean isStrict ( )
return boolean

true - strict, false - non-strict.

                public function isStrict(): bool
{
    return $this->strict;
}

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