Final Class Yiisoft\Validator\ValidationContext
| Inheritance | Yiisoft\Validator\ValidationContext |
|---|
Validation context that might be taken into account when performing validation.
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| PARAMETER_PREVIOUS_RULES_ERRORED | 'yii-validator-previous-rules-errored' | A name of parameter indicating that previous rule in the set caused validation error. Used to allow skipping of the current rule: - in {@see \Yiisoft\Validator\Validator} for rules implementing {@see \Yiisoft\Validator\SkipOnErrorInterface}. - for {@see \Yiisoft\Validator\Rule\StopOnError} rule (no additional configuration is needed). | Yiisoft\Validator\ValidationContext |
| PARAMETER_VALUE_AS_ARRAY | 'yii-validator-value-as-array' |
A name of parameter storing validated value as array. For rules working with arrays it helps to prevent extra
conversion of a validated value to array. The parameter's value type is either array or null. null means
the original value must be used.
|
Yiisoft\Validator\ValidationContext |
Method Details
| public mixed __construct ( array $parameters = [], Yiisoft\Validator\PropertyTranslatorInterface|null $propertyTranslator = null ) | ||
| $parameters | array |
Arbitrary parameters. |
| $propertyTranslator | Yiisoft\Validator\PropertyTranslatorInterface|null |
Optional property translator instance to use.
If |
public function __construct(
private array $parameters = [],
private ?PropertyTranslatorInterface $propertyTranslator = null,
) {}
| public string getCapitalizedTranslatedProperty ( ) |
public function getCapitalizedTranslatedProperty(): string
{
return StringHelper::uppercaseFirstCharacter($this->getTranslatedProperty());
}
Get the current scope's data set the property belongs to.
| public Yiisoft\Validator\DataSetInterface getDataSet ( ) | ||
| return | Yiisoft\Validator\DataSetInterface |
Data set instance. |
|---|---|---|
public function getDataSet(): DataSetInterface
{
if ($this->dataSet === null) {
throw new RuntimeException('Data set in validation context is not set.');
}
return $this->dataSet;
}
Get the global data set.
| public Yiisoft\Validator\DataSetInterface getGlobalDataSet ( ) | ||
| return | Yiisoft\Validator\DataSetInterface |
Data set instance. |
|---|---|---|
public function getGlobalDataSet(): DataSetInterface
{
$this->requireValidator();
return $this->globalDataSet;
}
Get named parameter.
See also \Yiisoft\Arrays\ArrayHelper::getValue().
| public mixed getParameter ( string $name, mixed $default = null ) | ||
| $name | string |
Parameter name. |
| $default | mixed |
Default value to return in case parameter with a given name does not exist. |
| return | mixed |
Parameter value. |
|---|---|---|
public function getParameter(string $name, mixed $default = null): mixed
{
return ArrayHelper::getValue($this->parameters, $name, $default);
}
Get validated data set's property name.
| public string|null getProperty ( ) | ||
| return | string|null |
Validated data set's property name. |
|---|---|---|
public function getProperty(): ?string
{
return $this->property;
}
| public string|null getPropertyLabel ( ) |
public function getPropertyLabel(): ?string
{
return $this->propertyLabel;
}
Get the raw validated data.
| public mixed getRawData ( ) | ||
| return | mixed |
The raw validated data. |
|---|---|---|
| throws | RuntimeException |
If validator is not set in validation context. |
public function getRawData(): mixed
{
$this->requireValidator();
return $this->rawData;
}
Get translated property name.
| public string getTranslatedProperty ( ) | ||
| return | string |
Translated property name. |
|---|---|---|
public function getTranslatedProperty(): string
{
$label = $this->propertyLabel ?? $this->property ?? 'value';
if ($this->propertyTranslator !== null) {
return $this->propertyTranslator->translate($label);
}
if ($this->defaultPropertyTranslator !== null) {
return $this->defaultPropertyTranslator->translate($label);
}
return $label;
}
Check whether {@see $property} is missing in a {@see $dataSet}.
| public boolean isPropertyMissing ( ) | ||
| return | boolean |
Whether {@see $property} is missing in a {@see $dataSet}. |
|---|---|---|
public function isPropertyMissing(): bool
{
return $this->isDataSetMissing
|| ($this->property !== null && !$this->getDataSet()->hasProperty($this->property));
}
Set parameter value.
| public $this setParameter ( string $name, mixed $value ) | ||
| $name | string |
Parameter name. |
| $value | mixed |
Parameter value. |
| return | $this |
The same instance of validation context. |
|---|---|---|
public function setParameter(string $name, mixed $value): self
{
$this->parameters[$name] = $value;
return $this;
}
| public self setPropertyLabel ( string|null $label ) | ||
| $label | string|null | |
public function setPropertyLabel(?string $label): self
{
$this->propertyLabel = $label;
return $this;
}
Set property translator to use.
| public $this setPropertyTranslator ( Yiisoft\Validator\PropertyTranslatorInterface|null $propertyTranslator ) | ||
| $propertyTranslator | Yiisoft\Validator\PropertyTranslatorInterface|null |
Property translator to use. If |
| return | $this |
The same instance of validation context. |
|---|---|---|
public function setPropertyTranslator(?PropertyTranslatorInterface $propertyTranslator): self
{
$this->propertyTranslator = $propertyTranslator;
return $this;
}
Validate data in current context.
| public Yiisoft\Validator\Result validate ( mixed $data, callable|iterable|object|string|null $rules = null ) | ||
| $data | mixed |
Data set to validate. If {@see \Yiisoft\Validator\RulesProviderInterface} instance provided and rules are not specified explicitly, they are read from the {@see \Yiisoft\Validator\RulesProviderInterface::getRules()}. |
| $rules | callable|iterable|object|string|null |
Rules to apply. If specified, rules are not read from data set even if it is an instance of {@see \Yiisoft\Validator\RulesProviderInterface}. |
| return | Yiisoft\Validator\Result |
Validation result. |
|---|---|---|
| throws | RuntimeException |
If validator is not set in validation context. |
public function validate(mixed $data, callable|iterable|object|string|null $rules = null): Result
{
$this->requireValidator();
$currentDataSet = $this->dataSet;
$currentProperty = $this->property;
$isCurrentDataSetMissing = $this->isDataSetMissing;
$currentParameters = $this->parameters;
$currentDefaultPropertyTranslator = $this->defaultPropertyTranslator;
// The lack of a property means that in the context of further validation there is no data set at all.
$this->isDataSetMissing = $this->isPropertyMissing();
if ($data instanceof PropertyTranslatorProviderInterface) {
$this->defaultPropertyTranslator = $data->getPropertyTranslator() ?? $currentDefaultPropertyTranslator;
}
$result = $this->validator->validate($data, $rules, $this);
$this->dataSet = $currentDataSet;
$this->property = $currentProperty;
$this->isDataSetMissing = $isCurrentDataSetMissing;
$this->parameters = $currentParameters;
$this->defaultPropertyTranslator = $currentDefaultPropertyTranslator;
return $result;
}
Signup or Login in order to comment.