Blogger Information
Blog 56
fans 3
comment 1
visits 50672
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
thinkphp验证器——2018年5月27日
沈斌的博客
Original
754 people have browsed it

thinkphp 验证器

application/index/controller/Verify.php

实例

<?php
namespace app\index\controller;

use think\Controller;
use app\index\validate\Staff;
use think\Validate;

class Verify extends Controller
{
	public function demo1()
	{
		$data=[
			'name'=>'er345',
			'sex'=>0,
			'age'=>56,
			'salary'=>19000
		];

		$validate=new Staff();
		if (!$validate->check($data)){
			dump($validate->getError());
		} else {
			return '验证通过';
		}
	}
}

运行实例 »

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

application/index/validate/Staff.php

实例

<?php
namespace app\index\validate;

use think\Validate;

class Staff extends Validate
{
	protected $rule=[
		'name'=>'require|min:4|max:10',
		'sex'=>'in:0,1',
		'age'=>'require|between:18,60',
		'salary'=>'require|gt:2000'
	];

	// wrong msg
	protected $message=[
		'name.require'=>'员工姓名不为空',
		'name.min'=>'姓名长度不小于4',
		'name.max'=>'姓名长度不大于10',
		'sex.in'=>'性别只可以选择男女',
		'age.require'=>'年龄为必填',
		'age.between'=>'年龄在18岁到60岁',
		'salary.require'=>'收入为必填',
		'salary.gt'=>'收入需要大于2000'
	];
}

运行实例 »

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

url 访问 http://tp.top/index.php/index/verify/demo1


全部验证通过显示 验证通过,没有验证通过根据 application/index/validate/Staff.php,$message 提示错误信息

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