ThinkPHP框架 validate用法

1.目录结构

 2.控制器引用

 protected $validate;public function __construct(){$this->validate = new IndexValidate();//引入该控制器对应的验证规则parent::__construct();}/**验证前端提交的post值*
*/
public function Index()
{$posts = $this->request->post();$this->validate->scene("index")->check($posts) || $this->error($this->validate->getError(), [], 202);
}

3.validate文件


class IndexValidate extends Validate
{//规则定义protected $rule = ["page" => "require|egt:1",// >=1"per_page" => "require|elt:15|egt:1"// 1= ["page", "per_page"]];
}

4.validate规则

protected static $typeMsg = ['require'     => ':attribute require','must'        => ':attribute must','number'      => ':attribute must be numeric','integer'     => ':attribute must be integer','float'       => ':attribute must be float','boolean'     => ':attribute must be bool','email'       => ':attribute not a valid email address','mobile'      => ':attribute not a valid mobile','array'       => ':attribute must be a array','accepted'    => ':attribute must be yes,on or 1','date'        => ':attribute not a valid datetime','file'        => ':attribute not a valid file','image'       => ':attribute not a valid image','alpha'       => ':attribute must be alpha','alphaNum'    => ':attribute must be alpha-numeric','alphaDash'   => ':attribute must be alpha-numeric, dash, underscore','activeUrl'   => ':attribute not a valid domain or ip','chs'         => ':attribute must be chinese','chsAlpha'    => ':attribute must be chinese or alpha','chsAlphaNum' => ':attribute must be chinese,alpha-numeric','chsDash'     => ':attribute must be chinese,alpha-numeric,underscore, dash','url'         => ':attribute not a valid url','ip'          => ':attribute not a valid ip','dateFormat'  => ':attribute must be dateFormat of :rule','in'          => ':attribute must be in :rule','notIn'       => ':attribute be notin :rule','between'     => ':attribute must between :1 - :2','notBetween'  => ':attribute not between :1 - :2','length'      => 'size of :attribute must be :rule','max'         => 'max size of :attribute must be :rule','min'         => 'min size of :attribute must be :rule','after'       => ':attribute cannot be less than :rule','before'      => ':attribute cannot exceed :rule','afterWith'   => ':attribute cannot be less than :rule','beforeWith'  => ':attribute cannot exceed :rule','expire'      => ':attribute not within :rule','allowIp'     => 'access IP is not allowed','denyIp'      => 'access IP denied','confirm'     => ':attribute out of accord with :2','different'   => ':attribute cannot be same with :2','egt'         => ':attribute must greater than or equal :rule','gt'          => ':attribute must greater than :rule','elt'         => ':attribute must less than or equal :rule','lt'          => ':attribute must less than :rule','eq'          => ':attribute must equal :rule','unique'      => ':attribute has exists','regex'       => ':attribute not conform to the rules','method'      => 'invalid Request method','token'       => 'invalid token','fileSize'    => 'filesize not match','fileExt'     => 'extensions to upload is not allowed','fileMime'    => 'mimetype to upload is not allowed',


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部