ensostudio/doctrine-entity-validator
最新稳定版本:1.1.3
Composer 安装命令:
composer require ensostudio/doctrine-entity-validator
包简介
Entity validator for Doctrine ORM 3
README 文档
README
By default, entity validation based on attached \Doctrine\ORM\Mapping\Column attributes and attributes inherited
ColumnValidator interface.
Also, you can add custom validators by EntityValidator::addValidator() or create instance of ColumnValidator interface.
Validator skip validation:
- If
Columnattribute declared as notupdatableand/orinsertable - Validation on persist/insert and property have
\Doctrine\ORM\Mapping\Idattribute
Validator checks by column type:
- If property value is null (or not defined), but
Columnattribute not declared asnullableor/and don't have default value (options: ['default' => '...']) - If
Columnattribute have numeric type (integer, float, decimal and etc.):- If defined
unsignedoption, then property value must be more than zero - If type
decimaland definedprecision, then check size of value
- If defined
- If
Columnattribute have string type (sting, text and etc.):- If defined
fixedoption andlength, then check string length - If defined only
length, then check string length
- If defined
- If
Columnattribute have enum type:- If defined
enumType, then check is proprerty value is declared in enum class
- If defined
ColumnValidator attributes:
Events
The postValidate(EntityValidator::EVENT_POST_VALIDATE) event triggers after successful validation, the syntax of
listener method: function (LifecycleEventArgs $args): void.
Examples
Validates Product entity before insert/update data:
use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use \EnsoStudio\Doctrine\ORM\ColumnValidators; use EnsoStudio\Doctrine\ORM\EntityValidator; use EnsoStudio\Doctrine\ORM\EntityValidationException; #[ORM\Entity] #[ORM\Table(name: 'products')] #[ORM\HasLifecycleCallbacks] class Product { ... #[ORM\Column(type: Types::STRING, length: 200)] #[ColumnValidators\MinLength(2)] #[ColumnValidators\Slug] private string $slug; #[ORM\Column(type: Types::STRING, length: 150)] #[ColumnValidators\Type('print')] private string $name; #[ORM\PrePersist] public function beforeInsert(): void { $validator = new EntityValidator($this); // Callback same to ColumnValidators\MinLength(3) $validator->addValidator( 'name', static function (string $propertyValue, string $propertyName, object $entity) { if (mb_strlen($propertyValue) < 3) { throw new EntityValidationException( ['% less than 3 characters', $propertyName], $propertyName, $entity ); } } ); $validator->validate(); } #[ORM\PreUpdate] public function beforeUpdate(): void { $validator = new EntityValidator($this); ... $validator->validate(true); } }
Or you can use EntityValidationSubscriber to validates all entities:
use Doctrine\ORM\EntityManager; use EnsoStudio\Doctrine\ORM\EntityValidationSubscriber; ... $entityManager = new EntityManager($connection, $config); $entityManager->getEventManager() ->addEventSubscriber(new EntityValidationSubscriber(true));
Requirements
- PHP >= 8.1 (with
mbstringextension) - doctrine/orm >= 3.3
Installation
If you do not have Composer, you may install it by following the instructions at getcomposer.org.
You can then install this library using the following command:
composer require ensostudio/doctrine-entity-validator
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2025-05-01