pravaler / validator
使用symfony组件进行验证,但采用laravel风格
1.1.2
2018-07-20 16:13 UTC
Requires
- php: ^7.1.3
- symfony/validator: ~4.0
Requires (Dev)
- phpunit/phpunit: 6.5.9
This package is not auto-updated.
Last update: 2024-09-29 05:42:03 UTC
README
使用Laravel风格创建一个使用Synfony验证的组件
支持的Symfony版本
4.x Symfony
安装
下载包
$ composer require pravaler/validator
使用
示例
<?php namespace App\Controller; use Pravaler\Component\Validator\Validator; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class PostController extends Controller { public function savePost(Request $request) { $data = $request->request->all(); $validator = new Validator([ 'name' => 'required', 'author' => 'required', 'email' => 'required|email', 'description' => 'required|size:1:500' ]); $validator->validate($data); if (!$validator->isValid()) { return $this->render("post/index.html.twig", [ 'errors' => $validator->getViolations() ]); } return $this->render("post/index.html.twig", [ 'message' => 'The post is valid!' ]); } }
可能的验证
Cpf
$validator = new Validator([ 'field' => 'cpf', ]);
$validator = new Validator([ 'field' => 'email', ]);
Max
$validator = new Validator([ 'field' => 'max:10', ]);
Min
$validator = new Validator([ 'field' => 'min:5', ]);
Numeric
$validator = new Validator([ 'field' => 'numeric', ]);
密码确认
// In order for the password confirm to take effect it is necessary to have in the data the password field $validator = new Validator([ 'field' => 'password_confirm', ]);
Required
$validator = new Validator([ 'field' => 'required', ]);
Size
$validator = new Validator([ 'field' => 'size:1:500', ]);
正则表达式
$validator = new Validator([ 'field' => 'regex:/[1-9]{2}\9\d{8}/', ]);
许可协议
此项目受MIT许可协议许可。有关更多信息,请参阅包含在此捆绑包中的许可协议文件。