徐晨 / form-validation
表单验证
1.0.0
2018-02-13 07:46 UTC
This package is not auto-updated.
Last update: 2024-09-29 01:58:21 UTC
README
为Lumen准备的表单验证插件
需求
{ "require": { "php": ">=5.6.0", "ext-mbstring": "*" } }
安装
在项目目录中运行以下命令
composer require xuchen/form-validation:dev-master
或在项目目录的composer.json的require中加入
"require": {
"xuchen/form-validation": "dev-master"
}
并运行
composer update
用法
在Lumen项目的app目录下新建MyValidation.php文件(你也可以放在Validations目录中,注意命名空间的对应)
<?php namespace App; use Xuchen\FormValidation\Validation; class MyValidation extends Validation { /** * 字段验证规则 */ protected function fieldsValidationRules() { return [ // 使用内置的方法验证字段 'real_name' => function() { // 返回的数组item为`验证方法` => `验证失败时需要返回的错误信息` return ['required' => '真实姓名必填']; }, 'mobile' => function() { return [ 'required' => '手机号必填', 'mobile' => '手机号格式不正确', ]; }, 'content' => function() { return [ 'maxzh:255' => '内容不能超过255个字符', ]; }, // 使用自定义的回调方法验证字段,返回false时表示验证失败 'type' => function() { $type = $this->getFormParam('type', 'link', 'strval'); if (!in_array($type, ['city', 'academy', 'link'])) { $this->_error_msg = '请检查类型'; return false; } } ]; } }
在控制器中调用验证方法
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\MyValidation; use Illuminate\Http\Request; class MyController extends Controller { public function testPostRoute(Request $request, MyValidation $validation) { if (!$validation->setFormParams($request->all())->validateFields()) { // 使用Validation::getErrorMsg()获取错误信息 return ['success' => false, 'error_msg' => $validation->getErrorMsg()]; } else { return ['success' => true, 'error_msg' => '']; } } }
更多文档见wiki页面。
作者
周徐晨,zhouxuchen1993@foxmail.com
许可证
xuchen/form-validation 在Apache许可证下可用。