0 follower

Final Class Yiisoft\Validator\ValidationContext

InheritanceYiisoft\Validator\ValidationContext

Validation context that might be taken into account when performing validation.

Constants

Hide inherited 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

Hide inherited methods

__construct() public method

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 null is provided, or it's not specified, a default translator passed through {@see \Yiisoft\Validator\setContextDataOnce()} is used.

                public function __construct(
    private array $parameters = [],
    private ?PropertyTranslatorInterface $propertyTranslator = null,
) {}

            
getCapitalizedTranslatedProperty() public method

public string getCapitalizedTranslatedProperty ( )

                public function getCapitalizedTranslatedProperty(): string
{
    return StringHelper::uppercaseFirstCharacter($this->getTranslatedProperty());
}

            
getDataSet() public method

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

            
getGlobalDataSet() public method

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

            
getParameter() public method

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

            
getProperty() public method

Get validated data set's property name.

public string|null getProperty ( )
return string|null

Validated data set's property name. null if a single value is validated.

                public function getProperty(): ?string
{
    return $this->property;
}

            
getPropertyLabel() public method

public string|null getPropertyLabel ( )

                public function getPropertyLabel(): ?string
{
    return $this->propertyLabel;
}

            
getRawData() public method

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

            
getTranslatedProperty() public method

Get translated property name.

public string getTranslatedProperty ( )
return string

Translated property name. value if a single value is validated and a label is not set.

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

            
isPropertyMissing() public method

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

            
setParameter() public method

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

            
setPropertyLabel() public method

public self setPropertyLabel ( string|null $label )
$label string|null

                public function setPropertyLabel(?string $label): self
{
    $this->propertyLabel = $label;
    return $this;
}

            
setPropertyTranslator() public method

Set property translator to use.

public $this setPropertyTranslator ( Yiisoft\Validator\PropertyTranslatorInterface|null $propertyTranslator )
$propertyTranslator Yiisoft\Validator\PropertyTranslatorInterface|null

Property translator to use. If null, translator passed in {@see \Yiisoft\Validator\setContextData()} will be used.

return $this

The same instance of validation context.

                public function setPropertyTranslator(?PropertyTranslatorInterface $propertyTranslator): self
{
    $this->propertyTranslator = $propertyTranslator;
    return $this;
}

            
validate() public method

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