0 follower

Final Class Yiisoft\Validator\Error

InheritanceYiisoft\Validator\Error

A class representing validation error. It's added in a rule handler or via {@see Callback} rule to the {@see Result} to form the complete list of errors for a single validation.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Error
getMessage() A getter for {@see $message} property. Returns raw (non-translated) validation error message. Yiisoft\Validator\Error
getMessageProcessing() Returns error message processing type. Yiisoft\Validator\Error
getParameters() A getter for {@see $parameters} property. Returns parameters used for {@see $message} translation. Yiisoft\Validator\Error
getValuePath() A getter for {@see $valuePath} property. Returns a sequence of keys determining where a value caused the validation error is located within a nested structure. Yiisoft\Validator\Error

Constants

Hide inherited constants

Constant Value Description Defined By
MESSAGE_FORMAT 1 Yiisoft\Validator\Error
MESSAGE_NONE 0 Yiisoft\Validator\Error
MESSAGE_TRANSLATE 2 Yiisoft\Validator\Error

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string|\Stringable $message, array $parameters = [], array $valuePath = [], integer $messageProcessing self::MESSAGE_TRANSLATE )
$message string|\Stringable

The raw validation error message. Can be a simple text or a template with placeholders enclosed in curly braces (}). In the end of the validation it will be translated using configured translator. {@see \Yiisoft\Translator\SimpleMessageFormatter} is usually enough, but for more complex translations {@see \Yiisoft\Translator\IntlMessageFormatter} can be used (requires "intl" PHP extension). Examples:

  • 'The value must be a string.' - simple text, works with both {@see \Yiisoft\Translator\SimpleMessageFormatter} and {@see \Yiisoft\Translator\IntlMessageFormatter}.
  • 'The value must be "{true}".' - simple substitution, works with both formatters.
  • `'This value must contain at least {min, number} {min, plural, one{item} other{items}}.' - plural form, works with both formatters.
  • 'You are {position, selectordinal, one {#st} two {#nd} few {#rd} other {#th}} in the queue.' - more complex syntax, works only with {@see \Yiisoft\Translator\IntlMessageFormatter}, requires "intl".
$parameters array

Parameters used for {@see $message} translation - a mapping between parameter names and values. Note that only scalar or null values are allowed.

$valuePath array

A sequence of keys determining where a value caused the validation error is located within a nested structure. Examples of different value paths:

$data = [
    [
        1,
        'text', // The value path is [0, 1].
    ],
    'post' => [
        'title' => 'Yii3 Overview 3', // The value path is ['post', 'title'].
        'files' => [
            [
                'url' => '...', // The value path is ['post', 'files', 0, 'url'].
            ],
        ],
    ],
];

A value without nested structure won't have a path at all (it will be an empty array).

$messageProcessing integer

Message processing type:

  • Error::MESSAGE_NONE - without post-processing;
  • Error::MESSAGE_FORMAT - format message;
  • Error::MESSAGE_TRANSLATE - translate message (translator do formatting also).

                public function __construct(
    string|Stringable $message,
    private readonly array $parameters = [],
    private readonly array $valuePath = [],
    private readonly int $messageProcessing = self::MESSAGE_TRANSLATE,
) {
    $this->message = (string) $message;
}

            
getMessage() public method

A getter for {@see $message} property. Returns raw (non-translated) validation error message.

public string getMessage ( )
return string

A simple text or a template used for translation.

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

            
getMessageProcessing() public method

Returns error message processing type.

public integer getMessageProcessing ( )

                public function getMessageProcessing(): int
{
    return $this->messageProcessing;
}

            
getParameters() public method

A getter for {@see $parameters} property. Returns parameters used for {@see $message} translation.

public array getParameters ( )
return array

A mapping between parameter names and values.

                public function getParameters(): array
{
    return $this->parameters;
}

            
getValuePath() public method

A getter for {@see $valuePath} property. Returns a sequence of keys determining where a value caused the validation error is located within a nested structure.

public array getValuePath ( string|null $escape null )
$escape string|null

Symbol that will be escaped with a backslash char (\) in path elements. When it's null path is returned without escaping.

return array

A list of keys for nested structures or an empty array otherwise.

                public function getValuePath(?string $escape = null): array
{
    if ($escape === null) {
        return $this->valuePath;
    }
    if (mb_strlen($escape) !== 1) {
        throw new InvalidArgumentException('Escape symbol must be exactly one character.');
    }
    return array_map(
        static fn($key): string => str_replace($escape, '\\' . $escape, (string) $key),
        $this->valuePath,
    );
}