博客列表 >自定义验证器-2018年05月29日

自定义验证器-2018年05月29日

植树青年小江同志的博客
植树青年小江同志的博客原创
2018年05月29日 18:00:45649浏览

验证器部分

实例

<?php

namespace app\validate;

use think\Validate;

class User extends Validate{

    // 验证规则
    protected $rule = [
        'name' => 'require|min:5|max:15',
        'email' => 'require|email|length:5,40',
        'password' => 'require|length:6,50',
    ];

    // 自定义错误信息

    protected $message = [
        'name.require' => '姓名不能为空',
        'name.min' => '姓名不能少于5字符',
        'name.max' => '姓名不能大于15个字符',
        'email.require' => 'Email不能为空',
        'email.email' => 'Email格式不正确',
        'email.length' => 'Email长度在5-40字符之间',
        'password.require' => '密码不能为空',
        'password.length' => '密码长度在20-50之间',
    ];
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

控制器部分

实例

<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use app\validate\User;
class Verify extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(User $user)
    {
        $data = ['name' => 'gakkispy', 'email' => 'mail@php.cn', 'password' => 'password'];

        $res= $user->check($data);

        return $res ? 'success' : $user->getError();
    }


}

运行实例 »

点击 "运行实例" 按钮查看在线实例


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议