yiisoft / hydrator-validator
Validating hydrator with raw data validation support
Fund package maintenance!
Opencollective
yiisoft
Installs: 32 231
Dependents: 2
Suggesters: 0
Security: 0
Stars: 12
Watchers: 16
Forks: 1
Open Issues: 1
Requires
- php: ^8.0
- yiisoft/hydrator: ^1.0
- yiisoft/validator: ^1.0|^2.0
Requires (Dev)
- maglnet/composer-require-checker: ^4.4
- phpunit/phpunit: ^9.5
- rector/rector: ^1.0.0
- roave/infection-static-analysis-plugin: ^1.16
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^4.30|^5.11
- yiisoft/test-support: ^3.0
This package is auto-updated.
Last update: 2024-11-20 08:06:51 UTC
README
Yii Validating Hydrator
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 decorator for hydrator that allows to validate:
- raw data of properties marked with
Validate
PHP attribute; - an 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. For further working with result, refer to
corresponding validator's guide section.
Validating hydrator usage example:
use Psr\Http\Message\RequestInterface; 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.