Get support for yiisoft/hydrator-validator

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

yiisoft/hydrator-validator

Latest Stable Version Total Downloads Build status codecov Mutation testing badge static analysis type-coverage psalm-level

The package provides a hydrator that also does validation, including raw data. It's useful when input data comes from a user, and you need to validate it and then put it into DTOs.

Requirements

  • PHP 8.0 or higher.

Installation

The package could be installed with Composer:

composer require yiisoft/hydrator-validator

General usage

Validating hydrator is a hydrator decorator that allows to validate raw data before passing it to the decorated hydrator and to validate object after creating or populating it.

To use it, the object being validated must implement ValidatedInputInterface. You can use ValidatedInputTrait to easily create such object. The validation rules for raw values of the object are defined with Validate PHP attribute.

Example of object:

use Yiisoft\Hydrator\Validator\Attribute\Validate;
use Yiisoft\Hydrator\Validator\ValidatedInputInterface;
use Yiisoft\Hydrator\Validator\ValidatedInputTrait;
use Yiisoft\Validator\Rule\Email;
use Yiisoft\Validator\Rule\Required;

final class InputDto implements ValidatedInputInterface 
{
    use ValidatedInputTrait;
    
    #[Email]
    private string $email;
    
    #[Validate(new Required())]
    private string $name;
}

Validation result could be obtained via getValidationResult() method.

Validating hydrator usage example:

use Yiisoft\Hydrator\HydratorInterface;
use Yiisoft\Hydrator\Validator\ValidatingHydrator;

public function actionEdit(RequestInterface $request, ValidatingHydrator $hydrator): ResponseInterface
{
    $data = $request->getParsedBody();    
    $inputDto = $hydrator->create(InputDto::class, $data);
    
    if (!$inputDto->getValidationResult()->isValid()) {
        // Validation didn't pass :(
    }
    
    // Everything is fine. You can use $inputDto now.    
}

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

License

The Yii Validating Hydrator is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2