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 Yiisoft\Validator\Validator for rules implementing Yiisoft\Validator\SkipOnErrorInterface. - for 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 __construct( array $parameters = [], Yiisoft\Validator\PropertyTranslatorInterface|null $propertyTranslator null ): mixed
$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 \Yiisoft\Validator\setContextDataOnce() is used.

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

            
getCapitalizedTranslatedProperty() public method

public getCapitalizedTranslatedProperty( ): string

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

            
getDataSet() public method

Get the current scope's data set the property belongs to.

public getDataSet( ): Yiisoft\Validator\DataSetInterface
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 getGlobalDataSet( ): Yiisoft\Validator\DataSetInterface
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 getParameter( string $name, mixed $default null ): mixed
$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 getProperty( ): string|null
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 getPropertyLabel( ): string|null

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

            
getRawData() public method

Get the raw validated data.

public getRawData( ): mixed
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 getTranslatedProperty( ): string
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 $property is missing in a $dataSet.

public isPropertyMissing( ): boolean
return boolean

Whether $property is missing in a $dataSet.

                public function isPropertyMissing(): bool
{
    return $this->isDataSetMissing
        || ($this->property !== null && !$this->getDataSet()->hasProperty($this->property));
}

            
setParameter() public method

Set parameter value.

public setParameter( string $name, mixed $value ): $this
$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 setPropertyLabel( string|null $label ): self
$label string|null

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

            
setPropertyTranslator() public method

Set property translator to use.

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

Property translator to use. If null, translator passed in \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 validate( mixed $data, callable|iterable|object|string|null $rules null ): Yiisoft\Validator\Result
$data mixed

Data set to validate. If Yiisoft\Validator\RulesProviderInterface instance provided and rules are not specified explicitly, they are read from the 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 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;
}