paliari / doctrine-validator
Doctrine ORM高级模型验证器(基于ActiveRecord)
Requires
- php: ^8.0
- doctrine/orm: ^2.11
- paliari/php-utils: ^2.0
Requires (Dev)
- doctrine/annotations: ^1.13
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-21 23:25:40 UTC
README
安装
composer require paliari/doctrine-validator
配置
在模型中包含TraitValidatorModel
// Create your model extended to AbstractRansackModel. class YourModel { use \Paliari\Doctrine\TraitValidatorModel; //... fields ... /** * Override the method getEm is required. * You must return your EntityManager in this method * * @return \Doctrine\ORM\EntityManager */ public static function getEM() { // return EntityManager } }
或者您可以让模型类继承自AbstractValidatorModel
class YourModel extends \Paliari\Doctrine\AbstractValidatorModel { //... fields ... /** * Override the method getEm is required. * You must return your EntityManager in this method * * @return \Doctrine\ORM\EntityManager */ public static function getEM() { // return EntityManager } }
初始化验证器
在您的bootstrap中添加
$eventManager = $entityManager->getEventManager(); $eventManager->addEventSubscriber(new \Paliari\Doctrine\ModelValidatorEventSubscriber());
用法
验证器
在模型中创建一个protected static
属性进行验证。查看可用选项和示例
-
validates_presence_of
-
选项:
if
,unless
,on
-
示例
protected static $validates_presence_of = [ 'email', 'last_login' => ['on' => 'update'], 'nike_name' => ['unless' => 'name'] ];
-
-
validates_size_of 等同于validates_length_of
-
选项:
minimum
,maximum
,in|within
-
示例
protected static $validates_length_of = [ 'name' => ['minimum' => 10, 'maximum' => 100], 'nike_name' => ['in' => [3, 20]] ];
选项
maximum
会自动设置为字段文档块中设置的最大值
-
-
validates_inclusion_of
-
选项:
in|within
,allow_nil
,allow_blank
-
示例
protected static $validates_inclusion_of = [ 'type' => ['in' => [1, 2, 3, 4]], 'value' => ['in' => [1, 2, 3, 4], 'allow_nil' => true], 'field_name' => ['in' => ['a', 'b', 'c'], 'allow_blank' => true] ];
布尔字段会自动设置为
true|false
-
-
validates_exclusion_of
-
选项:
in|within
,allow_nil
,allow_blank
-
示例
protected static $validates_exclusion_of = [ 'field_name' => ['in' => ['a', 'b', 'c'], 'allow_blank' => true] ];
-
-
validates_format_of
-
选项:
with
,without
- 值:
email
,url
,integer
,boolean
,ip
,/[0-9a-z]/
- 值:
-
示例
protected static $validates_format_of = [ 'email' => ['with' => 'email'], 'field_url' => ['with' => 'url'], 'field_name' => ['without' => 'float'] ];
-
-
validates_numericality_of
-
选项:
greater_than
,greater_than_or_equal_to
,less_than
,less_than_or_equal_to
,equal_to
,other_than
,only_integer
-
示例
protected static $validates_numericality_of = [ 'ammount' => ['greater_than' => 5], 'another_field' => ['only_integer' => true] ];
-
-
validates_uniqueness_of
-
选项:
scope
-
示例
protected static $validates_uniqueness_of = [ 'email', 'number' => ['scope' => ['year']], ];
-
-
validates_custom
-
示例
protected static $validates_custom = ['yourMethodName', 'otherYourMethodName']; public function yourMethodName() { if ($name == 'example') { $this->errors->add('name', '"name" cannot be "example"'); } } public function otherYourMethodName() { // Do something here }
-
每个验证器都支持以下默认选项列表:'if', 'unless', 'on', 'allow_nil'或'allow_blank'
回调
在模型中创建一个包含回调的protected static
属性。查看可用选项和示例
-
before_validation 验证前执行
-
示例
protected static $before_validation = ['yourCallbackName']; public function yourCallbackName() { /* Do something here */}
-
-
after_validation 验证后执行
-
示例
protected static $after_validation = ['anotherCallbackName'];
-
-
before_validation_on_create 仅创建前验证
-
示例
protected static $before_validation_on_create = ['yourCallbackName'];
-
-
after_validation_on_create 仅创建后验证
-
示例
protected static $after_validation_on_create = ['yourCallbackName'];
-
-
before_validation_on_update 仅更新前验证
-
示例
protected static $before_validation_on_update = ['yourCallbackName'];
-
-
after_validation_on_update 仅更新后验证
-
示例
protected static $after_validation_on_update = ['yourCallbackName'];
-
-
before_validation_on_remove 仅删除前验证
-
示例
protected static $before_validation_on_remove = ['yourCallbackName'];
-
-
after_validation_on_remove 仅删除后验证
-
示例
protected static $after_validation_on_remove = ['yourCallbackName'];
-
自定义验证器
要添加自定义验证器,只需在模型中使用静态方法ModelName::addCustomValidator,传入一个接收参数为$model的callable
要添加自定义验证器,只需在模型中使用静态方法ModelName :: addCustomValidator,传入一个接收参数为$model的可调用对象
示例
MyModel::addCustomValidator(function($model) { if ($model->isNewRecord()) { if (!MyValidator::instance()->isValid($model)) { foreach (MyValidator::instance()->getMessages() as $k => $item) { $model->errors->add($k, $item); } } } }); // ou MyModel::addCustomValidator('MyValidator::validate');
开发
安装依赖项
docker-compose -f docker-compose-cli.yml run --rm cli composer install
测试
docker-compose -f docker-compose-cli.yml run --rm cli ./vendor/bin/phpunit
发布新版本
# Generate a new tag docker-compose -f docker-compose-cli.yml run --rm cli ./vendor/bin/bump -g --version patch|minor|major git push && git push --tags