<?php /** * Created by PhpStorm. * User: Jason * Date: 2019/4/21 * Time: 19:54 */ namespace app\validate; use think\Validate; // 创建演员验证器 class Actors extends Validate { // 创建验证规则 protected $rule = [ 'name'=>'require|length:4,15', 'phone'=>'mobile', 'country'=>'require|length:2,30', 'birthday'=>'between:1,31', 'weight'=>'between:60,300', 'height'=>'between:100,200', ]; // 创建验证信息 protected $message = [ 'name.require'=>'演员名称不能为空', 'name.length'=>'演员名称必须4-15个字符之间', 'phone.mobile'=>'手机号码格式不正确', 'country.require'=>'演员国家不能为空', 'country.length'=>'演员国家必须是2-30个字符之间', 'birthday.between'=>'演员生日必须是1-31之间', 'weight.between'=>'演员体重必须是60-300之间', 'height.between'=>'演员盛高必须是100,200之间' ]; } ?> <?php /** * Created by PhpStorm. * User: Jason * Date: 2019/4/21 * Time: 20:02 */ namespace app\index\controller; // 引入验证类 use app\validate\Actors; use think\Controller; class Actor extends Controller { // 验证 public function vals() { // 实例化验证类 $validate = new Actors; // 创建数据 $data = [ 'name'=>'史提夫.史泰龙', 'phone'=>17688990987, 'country'=>'美国', 'birthday'=>'23', 'weight'=>90, 'height'=>188, ]; // 使用check方法验证 if(!$validate->check($data)) { return $validate->getError(); } return '验证成功'; } } ?>