Final Class Yiisoft\Validator\Result
| Inheritance | Yiisoft\Validator\Result |
|---|
Validation result that is used by both {@see ValidatorInterface} and {@see RuleHandlerInterface}.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| add() | Merges other validation results into the current one. | Yiisoft\Validator\Result |
| addError() | Add an error. | Yiisoft\Validator\Result |
| addErrorWithFormatOnly() | Add an error, the message of which does not require translation, but should be formatted. | Yiisoft\Validator\Result |
| addErrorWithoutPostProcessing() | Add an error, the message of which does not require any post-processing. | Yiisoft\Validator\Result |
| getCommonErrorMessages() | Get common error messages that are not attached to any property. | Yiisoft\Validator\Result |
| getErrorMessages() | Get errors messages as an array of strings. | Yiisoft\Validator\Result |
| getErrorMessagesIndexedByPath() | Get arrays of error messages indexed by property path. | Yiisoft\Validator\Result |
| getErrorMessagesIndexedByProperty() | Get arrays of error messages indexed by property name. | Yiisoft\Validator\Result |
| getErrors() | Yiisoft\Validator\Result | |
| getFirstErrorMessagesIndexedByPath() | Get strings of the first error messages for each property path. | Yiisoft\Validator\Result |
| getFirstErrorMessagesIndexedByProperty() | Get arrays of the first error messages for each property name. | Yiisoft\Validator\Result |
| getPropertyErrorMessages() | Get an array of error messages for the property specified. | Yiisoft\Validator\Result |
| getPropertyErrorMessagesByPath() | Get an array of error messages for the path specified. | Yiisoft\Validator\Result |
| getPropertyErrorMessagesIndexedByPath() | Get arrays of error messages for the property specified indexed by property path. | Yiisoft\Validator\Result |
| getPropertyErrors() | Get an array of error objects for the property specified. | Yiisoft\Validator\Result |
| isPropertyValid() | Whether property specified doesn't have any validation errors. | Yiisoft\Validator\Result |
| isValid() | Whether result doesn't have any validation errors. | Yiisoft\Validator\Result |
Method Details
Merges other validation results into the current one.
| public $this add ( Yiisoft\Validator\Result $results ) | ||
| $results | Yiisoft\Validator\Result |
Other results for merging. |
| return | $this |
Same instance of result. |
|---|---|---|
public function add(self ...$results): self
{
$appendErrors = [];
foreach ($results as $result) {
$appendErrors[] = $result->getErrors();
}
$this->errors = array_merge($this->errors, ...$appendErrors);
return $this;
}
Add an error.
| public $this addError ( string|\Stringable $message, array $parameters = [], array $valuePath = [] ) | ||
| $message | string|\Stringable |
The raw validation error message. See {@see \Yiisoft\Validator\Error::$message}. |
| $parameters | array |
Parameters used for {@see $message} translation - a mapping between parameter names and values. See {@see \Yiisoft\Validator\Error::$parameters}. |
| $valuePath | array |
A sequence of keys determining where a value caused the validation error is located within a nested structure. See {@see \Yiisoft\Validator\Error::$valuePath}. |
| return | $this |
Same instance of result. |
|---|---|---|
public function addError(string|Stringable $message, array $parameters = [], array $valuePath = []): self
{
$this->errors[] = new Error($message, $parameters, $valuePath);
return $this;
}
Add an error, the message of which does not require translation, but should be formatted.
See also addError().
| public $this addErrorWithFormatOnly ( string|\Stringable $message, array $parameters = [], array $valuePath = [] ) | ||
| $message | string|\Stringable | |
| $parameters | array | |
| $valuePath | array | |
| return | $this |
Same instance of result. |
|---|---|---|
public function addErrorWithFormatOnly(string|Stringable $message, array $parameters = [], array $valuePath = []): self
{
$this->errors[] = new Error($message, $parameters, $valuePath, Error::MESSAGE_FORMAT);
return $this;
}
Add an error, the message of which does not require any post-processing.
See also addError().
| public $this addErrorWithoutPostProcessing ( string|\Stringable $message, array $parameters = [], array $valuePath = [] ) | ||
| $message | string|\Stringable | |
| $parameters | array | |
| $valuePath | array | |
| return | $this |
Same instance of result. |
|---|---|---|
public function addErrorWithoutPostProcessing(string|Stringable $message, array $parameters = [], array $valuePath = []): self
{
$this->errors[] = new Error($message, $parameters, $valuePath, Error::MESSAGE_NONE);
return $this;
}
Get common error messages that are not attached to any property.
| public string[] getCommonErrorMessages ( ) | ||
| return | string[] |
Error messages. |
|---|---|---|
public function getCommonErrorMessages(): array
{
return $this->getPropertyErrorMessages('');
}
Get errors messages as an array of strings.
| public string[] getErrorMessages ( ) | ||
| return | string[] |
Array messages as strings. |
|---|---|---|
public function getErrorMessages(): array
{
return array_map(static fn(Error $error): string => $error->getMessage(), $this->errors);
}
Get arrays of error messages indexed by property path.
Each key is a dot-separated property path. Each value is an array of error message strings.
| public array getErrorMessagesIndexedByPath ( string $separator = '.', string|null $escape = '.' ) | ||
| $separator | string |
Property path separator. Dot is used by default. |
| $escape | string|null |
Symbol that will be escaped with a backslash char ( |
| return | array |
Arrays of error messages indexed by property path. |
|---|---|---|
public function getErrorMessagesIndexedByPath(string $separator = '.', ?string $escape = '.'): array
{
$errors = [];
foreach ($this->errors as $error) {
$stringValuePath = implode($separator, $error->getValuePath($escape));
$errors[$stringValuePath][] = $error->getMessage();
}
return $errors;
}
Get arrays of error messages indexed by property name.
| public array getErrorMessagesIndexedByProperty ( ) | ||
| return | array |
Arrays of error messages indexed by property name. |
|---|---|---|
| throws | InvalidArgumentException |
If top level property has a non-string type. |
public function getErrorMessagesIndexedByProperty(): array
{
$errors = [];
foreach ($this->errors as $error) {
$key = $error->getValuePath()[0] ?? '';
if (!is_string($key)) {
throw new InvalidArgumentException('Top level properties can only have string type.');
}
$errors[$key][] = $error->getMessage();
}
return $errors;
}
| public Yiisoft\Validator\Error[] getErrors ( ) | ||
| return | Yiisoft\Validator\Error[] |
Validation errors. |
|---|---|---|
public function getErrors(): array
{
return $this->errors;
}
Get strings of the first error messages for each property path.
Each key is a dot-separated property path. Each value is the first error message string for this path.
| public array getFirstErrorMessagesIndexedByPath ( string $separator = '.', string|null $escape = '.' ) | ||
| $separator | string |
Property path separator. Dot is used by default. |
| $escape | string|null |
Symbol that will be escaped with a backslash char ( |
| return | array |
Strings of error messages indexed by property path. |
|---|---|---|
public function getFirstErrorMessagesIndexedByPath(string $separator = '.', ?string $escape = '.'): array
{
$errors = [];
foreach ($this->errors as $error) {
$stringValuePath = implode($separator, $error->getValuePath($escape));
$errors[$stringValuePath] ??= $error->getMessage();
}
return $errors;
}
Get arrays of the first error messages for each property name.
| public array getFirstErrorMessagesIndexedByProperty ( ) | ||
| return | array |
Strings of error messages indexed by property name. |
|---|---|---|
| throws | InvalidArgumentException |
If top level property has a non-string type. |
public function getFirstErrorMessagesIndexedByProperty(): array
{
$errors = [];
foreach ($this->errors as $error) {
$key = $error->getValuePath()[0] ?? '';
if (!is_string($key)) {
throw new InvalidArgumentException('Top level properties can only have string type.');
}
$errors[$key] ??= $error->getMessage();
}
return $errors;
}
Get an array of error messages for the property specified.
| public string[] getPropertyErrorMessages ( string $property ) | ||
| $property | string | |
| return | string[] |
Error messages. |
|---|---|---|
public function getPropertyErrorMessages(string $property): array
{
$errors = [];
foreach ($this->errors as $error) {
$firstItem = $error->getValuePath()[0] ?? '';
if ($firstItem === $property) {
$errors[] = $error->getMessage();
}
}
return $errors;
}
Get an array of error messages for the path specified.
| public array getPropertyErrorMessagesByPath ( array $path ) | ||
| $path | array | |
public function getPropertyErrorMessagesByPath(array $path): array
{
$errors = [];
foreach ($this->errors as $error) {
if ($path === array_slice($error->getValuePath(), 0, count($path))) {
$errors[] = $error->getMessage();
}
}
return $errors;
}
Get arrays of error messages for the property specified indexed by property path.
Each key is a dot-separated property path. Each value is an array of error message strings.
| public array getPropertyErrorMessagesIndexedByPath ( string $property, string $separator = '.', string|null $escape = '.' ) | ||
| $property | string |
Property name. |
| $separator | string |
Property path separator. Dot is used by default. |
| $escape | string|null |
Symbol that will be escaped with a backslash char ( |
| return | array |
Arrays of error messages for the property specified indexed by property path. |
|---|---|---|
public function getPropertyErrorMessagesIndexedByPath(
string $property,
string $separator = '.',
?string $escape = '.',
): array {
$errors = [];
foreach ($this->errors as $error) {
$firstItem = $error->getValuePath()[0] ?? '';
if ($firstItem !== $property) {
continue;
}
$valuePath = implode($separator, array_slice($error->getValuePath($escape), 1));
$errors[$valuePath][] = $error->getMessage();
}
return $errors;
}
Get an array of error objects for the property specified.
| public Yiisoft\Validator\Error[] getPropertyErrors ( string $property ) | ||
| $property | string |
Property name. |
| return | Yiisoft\Validator\Error[] |
Array of error objects. |
|---|---|---|
public function getPropertyErrors(string $property): array
{
$errors = [];
foreach ($this->errors as $error) {
$firstItem = $error->getValuePath()[0] ?? '';
if ($firstItem === $property) {
$errors[] = $error;
}
}
return $errors;
}
Whether property specified doesn't have any validation errors.
| public boolean isPropertyValid ( string $property ) | ||
| $property | string |
Property name. |
| return | boolean |
Whether property is valid. |
|---|---|---|
public function isPropertyValid(string $property): bool
{
foreach ($this->errors as $error) {
$firstItem = $error->getValuePath()[0] ?? '';
if ($firstItem === $property) {
return false;
}
}
return true;
}
Signup or Login in order to comment.