0 follower

Final Class Yiisoft\FormModel\FormHydrator

InheritanceYiisoft\FormModel\FormHydrator

Form hydrator fills model with the data and optionally checks the data validity.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\FormModel\FormHydrator
populate() Fill the model with the data. Yiisoft\FormModel\FormHydrator
populateAndValidate() Fill the model with the data and validate it. Yiisoft\FormModel\FormHydrator
populateFromGet() Fill the model with the data from query parameters. Yiisoft\FormModel\FormHydrator
populateFromGetAndValidate() Fill the model with the data from query parameters and validate it. Yiisoft\FormModel\FormHydrator
populateFromPost() Fill the model with the data parsed from request body. Yiisoft\FormModel\FormHydrator
populateFromPostAndValidate() Fill the model with the data parsed from request body and validate it. Yiisoft\FormModel\FormHydrator
validate() Validate form model. Yiisoft\FormModel\FormHydrator

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Hydrator\HydratorInterface $hydrator, \Yiisoft\Validator\ValidatorInterface $validator )
$hydrator \Yiisoft\Hydrator\HydratorInterface

Hydrator to use to fill model with data.

$validator \Yiisoft\Validator\ValidatorInterface

Validator to use to check data before filling a model.

                public function __construct(
    private readonly HydratorInterface $hydrator,
    private readonly ValidatorInterface $validator,
) {
}

            
populate() public method

Fill the model with the data.

public boolean populate ( Yiisoft\FormModel\FormModelInterface $model, mixed $data, array|null $map null, boolean|null $strict null, string|null $scope null )
$model Yiisoft\FormModel\FormModelInterface

Model to fill.

$data mixed

Data to fill model with.

$map array|null

Map of object property names to keys in the data array to use for hydration. If not provided, it may be generated automatically based on presence of property validation rules and a strict setting.

$strict boolean|null

Whether to enable strict mode for filling data:

  • if false, fills everything that is in the data.
  • if null, fills data that is either defined in a map explicitly or allowed via validation rules.
  • if true, fills either only data defined explicitly in a map or only data allowed via validation rules but not both.
$scope string|null

Key to use in the data array as a source of data. Usually used when there are multiple forms at the same page. If not set, it equals to {@see \Yiisoft\FormModel\FormModelInterface::getFormName()}.

                public function populate(
    FormModelInterface $model,
    mixed $data,
    ?array $map = null,
    ?bool $strict = null,
    ?string $scope = null
): bool {
    if (!is_array($data)) {
        return false;
    }
    $scope ??= $model->getFormName();
    if ($scope === '') {
        $hydrateData = $data;
    } else {
        if (!isset($data[$scope]) || !is_array($data[$scope])) {
            return false;
        }
        $hydrateData = $data[$scope];
    }
    $this->hydrator->hydrate(
        $model,
        new ArrayData(
            $hydrateData,
            $this->createMap($model, $map, $strict),
            $strict ?? true
        )
    );
    return true;
}

            
populateAndValidate() public method

Fill the model with the data and validate it.

public boolean populateAndValidate ( Yiisoft\FormModel\FormModelInterface $model, mixed $data, array|null $map null, boolean|null $strict null, string|null $scope null )
$model Yiisoft\FormModel\FormModelInterface

Model to fill.

$data mixed

Data to fill model with.

$map array|null

Map of object property names to keys in the data array to use for hydration. If not provided, it may be generated automatically based on presence of property validation rules and a strict setting.

$strict boolean|null

If false, fills everything that is in the data. If null, fills data that is either defined in a map explicitly or allowed via validation rules. If false, fills only data defined explicitly in a map or only data allowed via validation rules but not both.

$scope string|null

Key to use in the data array as a source of data. Usually used when there are multiple forms at the same page. If not set, it equals to {@see \Yiisoft\FormModel\FormModelInterface::getFormName()}.

return boolean

Whether model is filled with data and is valid.

                public function populateAndValidate(
    FormModelInterface $model,
    mixed $data,
    ?array $map = null,
    ?bool $strict = null,
    ?string $scope = null
): bool {
    if (!$this->populate($model, $data, $map, $strict, $scope)) {
        return false;
    }
    return $this->validate($model)->isValid();
}

            
populateFromGet() public method

Fill the model with the data from query parameters.

public boolean populateFromGet ( Yiisoft\FormModel\FormModelInterface $model, \Psr\Http\Message\ServerRequestInterface $request, array|null $map null, boolean|null $strict null, string|null $scope null )
$model Yiisoft\FormModel\FormModelInterface

Model to fill.

$request \Psr\Http\Message\ServerRequestInterface

Request to get query parameters from.

$map array|null

Map of object property names to keys in the data array to use for hydration. If not provided, it may be generated automatically based on presence of property validation rules and a strict setting.

$strict boolean|null

If false, fills everything that is in the data. If null, fills data that is either defined in a map explicitly or allowed via validation rules. If true, fills only data defined explicitly in a map or only data allowed via validation rules but not both.

$scope string|null

Key to use in the data array as a source of data. Usually used when there are multiple forms at the same page. If not set, it equals to {@see \Yiisoft\FormModel\FormModelInterface::getFormName()}.

return boolean

Whether model is filled with data.

                public function populateFromGet(
    FormModelInterface $model,
    ServerRequestInterface $request,
    ?array $map = null,
    ?bool $strict = null,
    ?string $scope = null
): bool {
    if ($request->getMethod() !== 'GET') {
        return false;
    }
    return $this->populate($model, $request->getQueryParams(), $map, $strict, $scope);
}

            
populateFromGetAndValidate() public method

Fill the model with the data from query parameters and validate it.

public boolean populateFromGetAndValidate ( Yiisoft\FormModel\FormModelInterface $model, \Psr\Http\Message\ServerRequestInterface $request, array|null $map null, boolean|null $strict null, string|null $scope null )
$model Yiisoft\FormModel\FormModelInterface

Model to fill.

$request \Psr\Http\Message\ServerRequestInterface

Request to get query parameters from.

$map array|null

Map of object property names to keys in the data array to use for hydration. If not provided, it may be generated automatically based on presence of property validation rules and a strict setting.

$strict boolean|null

If false, fills everything that is in the data. If null, fills data that is either defined in a map explicitly or allowed via validation rules. If true, fills only data defined explicitly in a map or only data allowed via validation rules but not both.

$scope string|null

Key to use in the data array as a source of data. Usually used when there are multiple forms at the same page. If not set, it equals to {@see \Yiisoft\FormModel\FormModelInterface::getFormName()}.

return boolean

Whether model is filled with data and is valid.

                public function populateFromGetAndValidate(
    FormModelInterface $model,
    ServerRequestInterface $request,
    ?array $map = null,
    ?bool $strict = null,
    ?string $scope = null
): bool {
    if ($request->getMethod() !== 'GET') {
        return false;
    }
    return $this->populateAndValidate($model, $request->getQueryParams(), $map, $strict, $scope);
}

            
populateFromPost() public method

Fill the model with the data parsed from request body.

public boolean populateFromPost ( Yiisoft\FormModel\FormModelInterface $model, \Psr\Http\Message\ServerRequestInterface $request, array|null $map null, boolean|null $strict null, string|null $scope null )
$model Yiisoft\FormModel\FormModelInterface

Model to fill.

$request \Psr\Http\Message\ServerRequestInterface

Request to get parsed data from.

$map array|null

Map of object property names to keys in the data array to use for hydration. If not provided, it may be generated automatically based on presence of property validation rules and a strict setting.

$strict boolean|null

If false, fills everything that is in the data. If null, fills data that is either defined in a map explicitly or allowed via validation rules. If true, fills only data defined explicitly in a map or only data allowed via validation rules but not both.

$scope string|null

Key to use in the data array as a source of data. Usually used when there are multiple forms at the same page. If not set, it equals to {@see \Yiisoft\FormModel\FormModelInterface::getFormName()}.

                public function populateFromPost(
    FormModelInterface $model,
    ServerRequestInterface $request,
    ?array $map = null,
    ?bool $strict = null,
    ?string $scope = null
): bool {
    if ($request->getMethod() !== 'POST') {
        /** @infection-ignore-all */
        return false;
    }
    return $this->populate($model, $request->getParsedBody(), $map, $strict, $scope);
}

            
populateFromPostAndValidate() public method

Fill the model with the data parsed from request body and validate it.

public boolean populateFromPostAndValidate ( Yiisoft\FormModel\FormModelInterface $model, \Psr\Http\Message\ServerRequestInterface $request, array|null $map null, boolean|null $strict null, string|null $scope null )
$model Yiisoft\FormModel\FormModelInterface

Model to fill.

$request \Psr\Http\Message\ServerRequestInterface

Request to get parsed data from.

$map array|null

Map of object property names to keys in the data array to use for hydration. If not provided, it may be generated automatically based on presence of property validation rules and a strict setting.

$strict boolean|null

If false, fills everything that is in the data. If null, fills data that is either defined in a map explicitly or allowed via validation rules. If true, fills only data defined explicitly in a map or only data allowed via validation rules but not both.

$scope string|null

Key to use in the data array as a source of data. Usually used when there are multiple forms at the same page. If not set, it equals to {@see \Yiisoft\FormModel\FormModelInterface::getFormName()}.

return boolean

Whether model is filled with data and is valid.

                public function populateFromPostAndValidate(
    FormModelInterface $model,
    ServerRequestInterface $request,
    ?array $map = null,
    ?bool $strict = null,
    ?string $scope = null
): bool {
    if ($request->getMethod() !== 'POST') {
        /** @infection-ignore-all */
        return false;
    }
    return $this->populateAndValidate($model, $request->getParsedBody(), $map, $strict, $scope);
}

            
validate() public method

Validate form model.

public \Yiisoft\Validator\Result validate ( Yiisoft\FormModel\FormModelInterface $model )
$model Yiisoft\FormModel\FormModelInterface

Form model to validate.

return \Yiisoft\Validator\Result

Validation result.

                public function validate(FormModelInterface $model): Result
{
    return $this->validator->validate($model);
}