發(fā)表日期:2018-12 文章編輯:小燈 瀏覽次數(shù):3239
如果你需要在控制器中進(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)證代碼可以簡(jiǎ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ù):4937
日期:2018-12 瀏覽次數(shù):5219
日期:2018-12 瀏覽次數(shù):4295
日期:2018-12 瀏覽次數(shù):3650
日期:2018-12 瀏覽次數(shù):4045
日期:2018-12 瀏覽次數(shù):3616
日期:2018-12 瀏覽次數(shù):3661
日期:2018-12 瀏覽次數(shù):6482
日期:2018-12 瀏覽次數(shù):3423
日期:2018-12 瀏覽次數(shù):3532
日期:2018-12 瀏覽次數(shù):3652
日期:2018-12 瀏覽次數(shù):4777
日期:2018-12 瀏覽次數(shù):3166
日期:2018-12 瀏覽次數(shù):3486
日期:2018-12 瀏覽次數(shù):3294
日期:2018-12 瀏覽次數(shù):3169
日期:2018-12 瀏覽次數(shù):3549
日期:2018-12 瀏覽次數(shù):3410
日期:2018-12 瀏覽次數(shù):4505
日期:2018-12 瀏覽次數(shù):3960
日期:2018-12 瀏覽次數(shù):3478
日期:2018-12 瀏覽次數(shù):4261
日期:2018-12 瀏覽次數(shù):3261
日期:2018-12 瀏覽次數(shù):3238
日期:2018-12 瀏覽次數(shù):3200
日期:2018-12 瀏覽次數(shù):3354
日期:2018-12 瀏覽次數(shù):3647
日期:2018-12 瀏覽次數(shù):3433
日期:2018-12 瀏覽次數(shù):3375
日期:2018-12 瀏覽次數(shù):3429
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.