發(fā)表日期:2018-12 文章編輯:小燈 瀏覽次數(shù):3291
如果你需要在控制器中進(jìn)行驗(yàn)證,并且繼承了\think\Controller
的話,可以調(diào)用控制器類提供的validate
方法進(jìn)行驗(yàn)證,如下:
$result = $this->validate(['name'=> 'thinkphp','email' => 'thinkphp@qq.com',],['name'=> 'require|max:25','email' => 'email',]);if(true !== $result){// 驗(yàn)證失敗 輸出錯(cuò)誤信息dump($result);}
如果定義了驗(yàn)證器類的話,例如:
namespace app\index\validate;use think\Validate;class User extends Validate{protected $rule = ['name'=>'require|max:25','email' =>'email',];protected $message = ['name.require'=>'用戶名必須','email' =>'郵箱格式錯(cuò)誤',];protected $scene = ['add' =>['name','email'],'edit'=>['email'],];}
控制器中的驗(yàn)證代碼可以簡化為:
$result = $this->validate($data,'User');if(true !== $result){// 驗(yàn)證失敗 輸出錯(cuò)誤信息dump($result);}
如果要使用場(chǎng)景,可以使用:
$result = $this->validate($data,'User.edit');if(true !== $result){// 驗(yàn)證失敗 輸出錯(cuò)誤信息dump($result);}
在validate方法中還支持做一些前置的操作回調(diào),使用方式如下:
$result = $this->validate($data,'User.edit',[],[$this,'some']);if(true !== $result){// 驗(yàn)證失敗 輸出錯(cuò)誤信息dump($result);}
日期:2018-12 瀏覽次數(shù):4980
日期:2018-12 瀏覽次數(shù):5264
日期:2018-12 瀏覽次數(shù):4343
日期:2018-12 瀏覽次數(shù):3695
日期:2018-12 瀏覽次數(shù):4102
日期:2018-12 瀏覽次數(shù):3661
日期:2018-12 瀏覽次數(shù):3702
日期:2018-12 瀏覽次數(shù):6521
日期:2018-12 瀏覽次數(shù):3476
日期:2018-12 瀏覽次數(shù):3578
日期:2018-12 瀏覽次數(shù):3706
日期:2018-12 瀏覽次數(shù):4829
日期:2018-12 瀏覽次數(shù):3214
日期:2018-12 瀏覽次數(shù):3530
日期:2018-12 瀏覽次數(shù):3332
日期:2018-12 瀏覽次數(shù):3214
日期:2018-12 瀏覽次數(shù):3587
日期:2018-12 瀏覽次數(shù):3453
日期:2018-12 瀏覽次數(shù):4553
日期:2018-12 瀏覽次數(shù):4001
日期:2018-12 瀏覽次數(shù):3515
日期:2018-12 瀏覽次數(shù):4317
日期:2018-12 瀏覽次數(shù):3299
日期:2018-12 瀏覽次數(shù):3290
日期:2018-12 瀏覽次數(shù):3246
日期:2018-12 瀏覽次數(shù):3407
日期:2018-12 瀏覽次數(shù):3691
日期:2018-12 瀏覽次數(shù):3479
日期:2018-12 瀏覽次數(shù):3411
日期:2018-12 瀏覽次數(shù):3466
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.