谈表单验证案例之ThinkPHP3.2使用ThinkPHP5.0+的Validate类
2017-08-02 13:19
471 查看
对表单进行验证是非常繁琐,重复,但有不得不做的事情,自从用了laravel的验证类后,瞬间觉得脑洞打开,以前在javascript的有实现相关功能的函数,但没完整整理出封装出类的思路,下面由几个案例入手,最后在整理下该类实现的思路,tp5.0中的validate类有借鉴laravel的意思,下面的例子为在tp3.2中加载使用tp5.0中的validate类:
下载ThinkPHP5.0+,找到
复制到
并且重命名:
然后在框架 functions.php 文件里,添加大V方法。
functions.php 路径:
调用实例:
AllInvestRecord路径:
AllInvestRecordValidate类:
验证类使用方法,可以参照ThinkPHP5.0+的Validate类文档:
http://www.kancloud.cn/manual/thinkphp5/129352
下载ThinkPHP5.0+,找到
thinkphp\library\think\Validate.php
复制到
framework\ThinkPHP\Library\Think
并且重命名:
Validate.php -> Validate.class.php
然后在框架 functions.php 文件里,添加大V方法。
functions.php 路径:
framework\ThinkPHP\Common
/** * 实例化验证类 格式:[模块名/]验证器名 * @param string $name 资源地址 * @param string $layer 验证层名称 * @param string $common 公共模块名 * @return Object|false * @throws Exception * * @author chengbin */ function V($name = '', $layer = 'Validate', $common = 'Common') { if (empty($name)) { return new Think\Validate; } static $_validate = array(); $guid = $name . $layer; if (isset($_validate[$guid])) { return $_validate[$guid]; } $class = parse_res_name( $name, $layer ); if (class_exists($class)) { $validate = new $class; } else { if(!C('APP_USE_NAMESPACE')){ import('Common/'.$layer.'/'.$class); }else{ $class = '\\Common\\'.$layer.'\\'.$name.$layer; } if (class_exists($class)) { $validate = new $class; } else { throw new Exception('Validate Class Not Exists:' . $class); } } $_validate[$guid] = $validate; return $validate; }
调用实例:
$allInvestRecordValidate = V('AllInvestRecord'); if( !$allInvestRecordValidate->scene('ret')->check( $request ) ) { throw new \Exception( $allInvestRecordValidate->getError() ); }
AllInvestRecord路径:
common\Common\Validate\AllInvestRecordValidate.class.php
AllInvestRecordValidate类:
<?php namespace Common\Validate; class AllInvestRecordValidate extends \Think\Validate { protected $rule = [ 'loan_id' => 'require', ]; protected $message = [ 'loan_id.require' => '缺少参数 loan_id' ]; protected $scene = [ //流标场景 'ret' => ['loan_id'], ]; }
验证类使用方法,可以参照ThinkPHP5.0+的Validate类文档:
http://www.kancloud.cn/manual/thinkphp5/129352
相关文章推荐
- 使用thinkphp框架完成表单自动验证
- ThinkPHP 3.2的原始jq表单验证
- 使用AmazeUI做的一个表单案例(验证)
- PHP使用正则匹配进行表单验证案例
- 如何使用thinkphp的model来验证前端表单?
- thinkphp5.0自定义验证规则使用方法
- 学习thinkphp5.0验证类使用方法
- liferay中使用jQuery Validation Engine进行ajax验证,不通过也提交表单的解决方案
- thinkphp3.2 U方法 使用与注意事项
- 使用onsubmit实现表单验证
- Yii2.0模型层数据验证和thinkPHP3.2框架模型层数据验证对比
- [置顶] thinkPHP3.2中join与field,order,where的配合使用
- thinkphp 3.2 模型的使用示例
- JavaScript之jQuery-7 jQuery 使用插件(使用插件、日历插件、表单验证插件)
- 使用jQuery validate 验证注册表单
- Ajax中如何使用Session变量,Cookies可以用表单验证的方式获取并使用。
- Forms表单验证 使用经验及验证流程分析
- css翻译系列---(web表单验证之,使用jquery进行动画验证)
- PHP ThinkPHP 表单自动验证
- jquery validate表单验证插件的基本使用方法及功能拓展