Blogger Information
Blog 5
fans 0
comment 0
visits 2214
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
模型高级操作与验证器—2018年5月25日12时28分
候磊的博客
Original
405 people have browsed it
controller:
<?php 
namespace app\index\controller;

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

class Verify extends Controller
{
	public function test()
	{
		$data = [
			'name'=>'guanyu',
			'sex' => 1,
			'age' => 50,
			'salary' => 3400
		];

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

validate:
<?php 
namespace app\validate;

use think\Validate;

class Staff extends Validate
{
	protected $rule = [
		'name'=>'require|min:3|max:10',
		'sex' => 'require|in:0,1',
		'age' => 'require|between:20,50',
		'salary' => 'require|gt: 3000'
	];
	
	protected $message = [
		'name.require' => '姓名不能为空',
		'name.min' => '姓名长度不能少于3个字符',
		'name.max' => '姓名长度不能大于10个字符',
		'sex.require' => '性别不能为空',
		'sex.in' => '性别只能选择男或女',
		'age.require' => '年龄不能为空',
		'age.between' => '年龄必须在20到50之间',
		'salary.require' => '工资必须输入',
		'salary.gt' => '工资必须大于3000元'
	];
}


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