Blogger Information
Blog 28
fans 0
comment 0
visits 16648
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
自定义验证器-2018年05月29日
植树青年小江同志的博客
Original
660 people have browsed it

验证器部分

实例

<?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();
    }


}

运行实例 »

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


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post