0 follower

Final Class Yiisoft\Validator\Rule\UniqueIterable

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

Allows to define a set of rules for validating uniqueness of each element of an iterable.

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

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Rule\UniqueIterable
getDifferentTypesMessage() Error message used when validation fails because the validated iterable contains items of different types. Yiisoft\Validator\Rule\UniqueIterable
getHandler() Yiisoft\Validator\Rule\UniqueIterable
getIncorrectInputMessage() Gets error message used when validation fails because the validated value is not an iterable. Yiisoft\Validator\Rule\UniqueIterable
getIncorrectItemValueMessage() Error message used when validation fails because the validated iterable contains items with invalid values. Yiisoft\Validator\Rule\UniqueIterable
getMessage() Error message used when validation fails because the validated iterable contains duplicate items. Yiisoft\Validator\Rule\UniqueIterable
getName() Yiisoft\Validator\Rule\UniqueIterable
getOptions() Yiisoft\Validator\Rule\UniqueIterable
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 mixed __construct ( string $incorrectInputMessage '{Property} must be array or iterable.', string $incorrectItemValueMessage 'The allowed types for iterable\'s item values of {property} are ' 'integer, float, string, boolean and object implementing \\Stringable or \\DateTimeInterface.', string $differentTypesMessage 'All iterable items of {property} must have the same type.', string $message 'Every iterable\'s item of {property} must be unique.', boolean|callable|null $skipOnEmpty null, boolean $skipOnError false, Closure|null $when null )
$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.
$incorrectItemValueMessage string

Error message used when validation fails because the validated iterable contains items with invalid values. Only the following types are allowed: scalar (string, integer, float, boolean), objects implementing \Stringable or \DateTimeInterface interfaces.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {type}: the type of the iterable key being validated.
$differentTypesMessage string

Error message used when validation fails because the validated iterable contains items of different type.

You may use the following placeholders in the message:

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

Error message used when validation fails because the validated iterable contains duplicate items.

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 Each 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 Each 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 Each rule with all defined {@see $rules}. See {@see \Yiisoft\Validator\WhenInterface}.

                public function __construct(
    private string $incorrectInputMessage = '{Property} must be array or iterable.',
    private string $incorrectItemValueMessage = 'The allowed types for iterable\'s item values of {property} are '
    . 'integer, float, string, boolean and object implementing \Stringable or \DateTimeInterface.',
    private string $differentTypesMessage = 'All iterable items of {property} must have the same type.',
    private string $message = 'Every iterable\'s item of {property} must be unique.',
    bool|callable|null $skipOnEmpty = null,
    private bool $skipOnError = false,
    private ?Closure $when = null,
) {
    $this->skipOnEmpty = $skipOnEmpty;
}

            
getDifferentTypesMessage() public method

Error message used when validation fails because the validated iterable contains items of different types.

public string getDifferentTypesMessage ( )
return string

Error message / template.

                public function getDifferentTypesMessage(): string
{
    return $this->differentTypesMessage;
}

            
getHandler() public method

public string getHandler ( )

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

            
getIncorrectInputMessage() public method

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

public string getIncorrectInputMessage ( )
return string

Error message / template.

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

            
getIncorrectItemValueMessage() public method

Error message used when validation fails because the validated iterable contains items with invalid values.

public string getIncorrectItemValueMessage ( )
return string

Error message / template.

                public function getIncorrectItemValueMessage(): string
{
    return $this->incorrectItemValueMessage;
}

            
getMessage() public method

Error message used when validation fails because the validated iterable contains duplicate items.

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 ( )

                #[ArrayShape([
    'incorrectInputMessage' => 'array',
    'incorrectItemValueMessage' => 'array',
    'message' => 'array',
    'skipOnEmpty' => 'bool',
    'skipOnError' => 'bool',
])]
public function getOptions(): array
{
    return [
        'incorrectInputMessage' => [
            'template' => $this->incorrectInputMessage,
            'parameters' => [],
        ],
        'incorrectItemValueMessage' => [
            'template' => $this->incorrectItemValueMessage,
            'parameters' => [],
        ],
        'differentTypesMessage' => [
            'template' => $this->differentTypesMessage,
            '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;
}

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