承接 atk4/validate 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

atk4/validate

最新稳定版本:5.0.0

Composer 安装命令:

composer require atk4/validate

包简介

Agile Data - Validator add-on

README 文档

README

This is validator add-on for Agile Data (https://github.com/atk4/data).

It uses https://github.com/vlucas/valitron under the hood.

Code Climate StyleCI CodeCov Test Coverage Issue Count

License GitHub release

Usage

// add model fields
$model->addField('name');
$model->addField('age', ['type' => 'number']);
$model->addField('type', ['required' => true, 'enum' => ['dog', 'ball']]);
$model->addField('tail_length', ['type' => 'number']);

// add validator to your model
// also will register itself as $model->validator property
$v = new \Atk4\Validate\Validator($model);

// set simple validation rule for one field
// ->rule($field, $rules)
$v->rule('name', [ 'required', ['lengthMin', 3] ]);

// set multiple validation rules in one shot
// ->rules($array_of_rules) // [field=>rules]
$v->rules([
    'name' => ['required', ['lengthMin',3]],
    'age' => ['integer', ['min',0], ['max',99]],
    'tail_length' => ['integer', ['min',0]],
]);

// set validation rules based on value of another field
// if type=dog, then sets fields age and tail_length as required
// ->if($condition, $then_array_of_rules, $else_array_of_rules)
$v->if(['type'=>'dog'], [
    'age' => ['required'],
    'tail_length' => ['required'],
], [
    'tail_length' => [ ['equals',''] ], // balls don't have tail
]);

// you can also pass multiple conditions which will be treated as AND conditions
$v->if(['type'=>'dog', 'age'=>50], $rules_if_true, $rules_if_false);

// you can also set custom error message like this:
$v->rule('age', [ ['min', 3, 'message'=>'Common! {field} to small'] ]);
// and you will get this "Common! Age to small"

You can also pass callback instead of array of rules. Callback receives these parameters $field, $value, $args, $data and should return true/false.

See /tests folder for more examples.

统计信息

  • 总下载量: 8.18k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 8
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-12-13