返回 创建Actor...... 登陆

创建Actor验证器类,并进行验证!

JasonKim 2019-04-21 20:14:31 240
<?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 '验证成功';

    }
}
?>


最新手记推荐

• 用composer安装thinkphp框架的步骤 • 省市区接口说明 • 用thinkphp,后台新增栏目 • 管理员添加编辑删除 • 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网