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

Original 2019-04-21 20:14:31 254
abstract:<?php /**  * Created by PhpStorm.  * User: Jason  * Date: 2019/4/21  * Time: 19:54  */ namespace app\validate; use 
<?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 '验证成功';

    }
}
?>


Correcting teacher:天蓬老师Correction time:2019-04-22 10:22:18
Teacher's summary:创建验证的提示信息, 是比较麻烦的, 因为针对 每一个验证规则都要设置, 如果你的需要不是太性化, 其实框架内置的提示信息 基本上可以满足大部分场景下的需求的

Release Notes

Popular Entries