-
Notifications
You must be signed in to change notification settings - Fork 33
config rules like laravel
Inhere edited this page Apr 28, 2020
·
3 revisions
inhere/validate
也支持像Laravel一样配置验证规则
- 每条规则验证一个字段,但是可以同时配置多个验证器
如果你喜欢类似Yii方式的配置规则,也是可以的。 请参考前一篇文档
需要快速简便的使用验证时,可直接使用 Inhere\Validate\FieldValidation
use Inhere\Validate\FieldValidation; class SomeController { public function demoAction() { $v = FieldValidation::check($_POST,[ // add rule ['title', 'string|min:20|max:60'], ['freeTime', 'number|min:10'], ]); if ($v->isFail()) { var_dump($v->getErrors()); var_dump($v->firstError()); } // $postData = $v->all(); // 原始数据 $safeData = $v->getSafeData(); // 验证通过的安全数据 $db->save($safeData); } }
创建一个新的class,并继承 Inhere\Validate\FieldValidation
。用于一个(或一系列相关)请求的验证, 相当于 laravel 的 表单请求验证
此方式是最为完整的使用方式,可以配置规则,设置字段翻译,设置自定义的错误消息等
use Inhere\Validate\FieldValidation; class PageRequest extends FieldValidation { public function rules(): array { return [ // 必填并且 4 <= tagId <=567 ['tagId', 'required|min:4|max:567', 'filter' => 'int'], // title 必填并且 length >= 40 ['title', 'required|min:40', 'filter' => 'trim'], // 必填并且大于 0 ['freeTime', 'required|number'], // 含有前置条件 ['tagId', 'number', 'when' => function($data) { return isset($data['status']) && $data['status'] > 2; }], // 在验证前会先过滤转换为 int。并且仅会在指明场景名为 'scene1' 时规则有效 ['userId', 'number', 'on' => 'scene1', 'filter' => 'int'], ['username', 'string|min:6', 'on' => 'scene2', 'filter' => 'trim'], // 使用自定义正则表达式 ['username', 'required|regexp' ,'/^[a-z]\w{2,12}$/'], // 自定义验证器,并指定当前规则的消息 ['title', 'custom', 'msg' => '{attr} error msg!' ], // 直接使用闭包验证 ['status', function($status) { if (is_int($status) && $status > 3) { return true; } return false; }], // 标记字段是安全可靠的 无需验证 ['createdAt, updatedAt', 'safe'], ]; } // 定义不同场景需要验证的字段。 // 功能跟规则里的 'on' 类似,两者尽量不要同时使用,以免混淆。 public function scenarios(): array { return [ 'create' => ['user', 'pwd', 'code'], 'update' => ['user', 'pwd'], ]; } // 定义字段翻译 public function translates() { return [ 'userId' => '用户Id', ]; } // 自定义验证器的提示消息, 默认消息请看 {@see ErrorMessageTrait::$messages} public function messages() { return [ 'required' => '{attr} 是必填项。', // 可以直接针对字段的某个规则进行消息定义 'title.required' => 'O, 标题是必填项。are you known?', ]; } // 添加一个验证器。必须返回一个布尔值标明验证失败或成功 protected function customValidator($title) { // some logic ... // $this->getRaw('field'); 访问 data 数据 return true; // Or false; } }
- 使用
// 验证 POST 数据 $v = PageRequest::check($_POST); // 验证失败 if ($v->isFail()) { var_dump($v->getErrors()); var_dump($v->firstError()); } // 验证成功 ... $safeData = $v->getSafeData(); // 验证通过的安全数据 // $postData = $v->all(); // 原始数据 $db->save($safeData);