您现在的位置是:首页 > cms教程 > 易优cms教程易优cms教程

易优eyoucms表单验证场景使用方法

车杜炯2025-04-10易优cms教程已有人查阅

导读5.0.4 增加hasScene方法用于检查是否存在验证场景可以在定义验证规则的时候定义场景,并且验证不同场景的数据,例如:表示验证edit场景(该场景定义只需要验证name和age字段)。

5.0.4 增加hasScene方法用于检查是否存在验证场景可以在定义验证规则的时候定义场景,并且验证不同场景的数据,例如:
$rule = [
'name' => 'require|max:25',
'age' => 'number|between:1,120′,
'email' => 'email',
];
$msg = [
'name.require' => '名称必须',
'name.max' => '名称最多不能超过25个字符',
'age.number' => '年龄必须是数字',
'age.between' => '年龄只能在1-120之间',
'email' => '邮箱格式错误',
];
$data = [
'name' => 'thinkphp',
'age' => 10,
'email' => 'thinkphp@qq.com',
];
$validate = new Validate($rule);
$validate->scene('edit', ['name', 'age']);
$result = $validate->scene('edit')->check($data);
表示验证edit场景(该场景定义只需要验证name和age字段)。
如果使用了验证器,可以直接在类里面定义场景,例如:
namespace appindex alidate;
use thinkValidate;
class User extends Validate
{
protected $rule = [
'name' => 'require|max:25',
'age' => 'number|between:1,120′,
'email' => 'email',
];
protected $message = [
'name.require' => '名称必须',
'name.max' => '名称最多不能超过25个字符',
'age.number' => '年龄必须是数字',
'age.between' => '年龄只能在1-120之间',
'email' => '邮箱格式错误',
];
protected $scene = [
'edit' => ['name','age'],
];
}
然后再需要验证的地方直接使用 scene 方法验证
$data = [
'name' => 'thinkphp',
'age' => 10,
'email' => 'thinkphp@qq.com',
];
$validate = new ppindex alidateUser($rule);
$result = $validate->scene('edit')->check($data);
可以在定义场景的时候对某些字段的规则重新设置,例如:
namespace appindex alidate;
use thinkValidate;
class User extends Validate
{
protected $rule = [
'name' => 'require|max:25',
'age' => 'number|between:1,120′,
'email' => 'email',
];
protected $message = [
'name.require' => '名称必须',
'name.max' => '名称最多不能超过25个字符',
'age.number' => '年龄必须是数字',
'age.between' => '年龄只能在1-120之间',
'email' => '邮箱格式错误',
];
protected $scene = [
'edit' => ['name','age'=>'require|number|between:1,120′],
];
}
可以对场景设置一个回调方法,用于动态设置要验证的字段,例如:
$rule = [
'name' => 'require|max:25',
'age' => 'number|between:1,120′,
'email' => 'email',
];
$msg = [
'name.require' => '名称必须',
'name.max' => '名称最多不能超过25个字符',
'age.number' => '年龄必须是数字',
'age.between' => '年龄只能在1-120之间',
'email' => '邮箱格式错误',
];
$data = [
'name' => 'thinkphp',
'age' => 10,
'email' => 'thinkphp@qq.com',
];
$validate = new Validate($rule);
$validate->scene('edit', function($key,$data){
return 'email'==$key && isset($data['id'])? true : false;
});
$result = $validate->scene('edit')->check($data);

本文标签:

很赞哦! ()

相关源码

  • (自适应)绿色农业大型机械设备展示网站模板下载基于PbootCMS内核深度定制开发的农业机械行业专用模板。针对农机设备展示、产品参数说明等需求优化设计,突出农业机械行业特性查看源码
  • (PC+WAP)红色家装设计智能家居家具建材pbootcms网站源码下载本模板基于PbootCMS系统开发,为智能家居、家装设计及家具建材行业设计。采用现代化布局风格,突出家居设计行业特色,适合展示各类家居产品、设计方案和建材信息。查看源码
  • 粉色家政月嫂保姆公司pbootcms网站模板(PC+WAP)为家政服务、月嫂保姆企业打造的营销型解决方案,基于PbootCMS内核开发,采用温馨粉色主题传递行业温度。PHP7.0+高性能架构支持SQLite/MySQL双数据库查看源码
  • (自适应)帝国cms7.5模板新闻资讯门户带会员中心基于帝国CMS7.5内核开发的HTML5响应式模板,为新闻机构、媒体门户及资讯聚合平台设计。通过模块化布局实现图文混排查看源码
  • (pc+wap)pbootcms网站模板蓝色小程序网站开发公司基于PbootCMS内核开发的营销型门户模板,为小程序开发公司、电商软件企业打造。采用HTML5自适应架构,实现PC与手机端数据实时同步展示查看源码
  • (PC+WAP)玻璃钢不锈钢钢材环保设备pbootcms网站模板采用PbootCMS内核开发的响应式网站模板,为玻璃钢环保设备制造、不锈钢钢材贸易企业设计,适配产品展示及企业服务场景。通过自适应技术实现PC与WAP端数据实时同步查看源码
分享笔记 (共有 篇笔记)
验证码:

本栏推荐