当前位置: 首页 > 后端技术 > PHP

在非Laravel项目中使用验证器

时间:2023-03-30 02:28:36 PHP

安装composerrequireilluminate/validation引入提示信息项目根目录创建lang/zh_cn/validation.php文件':attributemustaccept','active_url'=>':attributemustbealegalURL','after'=>':attributemustbe日期在:date','after_or_equal'=>':attribute之后的日期必须在:date','alpha'=>':attributeCanonlycontainletters','alpha_dash'=>':attribute只能包含字母、数字、破折号或下划线','alpha_num'=>':attributecanonlycontainlettersandnumbers','array'=>':attributemustbeanarray','before'=>':attribute必须是早于:date','before_or_equal'=>':attribute的日期必须早于或等于:date','between'=>['numeric'=>':attribute必须介于:minto:max','file'=>':attributemustbebetween:minand:maxKB','string'=>':attributemustbebetween:minand:maxcharacters','array'=>':attributemustbebetween:minand:maxitems',],'boolean'=>':attributecharactermustbetrueorfalse,1or0','confirmed'=>':attributesecondaryconfirmationdoesnotmatch','date'=>':attributemustbeavaliddate','date_format'=>':attributedoesnotmatchthegivenformat:format','different'=>':attributemustbedifferentfrom:other','digits'=>':属性必须是:digits数字。','digits_between'=>':属性必须介于:最小和:最大数字之间','dimensions'=>':属性具有无效的图像大小','distinct'=>':attribute字段有重复值','email'=>':attribute必须是有效的电子邮件地址','exists'=>'所选的:attribute无效d'.','文件'=>':attributemustbeafile','filled'=>':attributefieldisrequired','image'=>':attributemustbeapictureinjpeg,png,bmporgif格式','in'=>'Theselected:attributeisinvalid','in_array'=>':attribute字段不存在于:other','integer'=>'The:attributemustbeaninteger','ip'=>':attribute必须是有效的IP地址。','json'=>':attribute必须是有效的JSON字符串','max'=>['numeric'=>':attribute的最大长度为:maxbits','file'=>':maximumofattributeis:max','string'=>':attribute的最大长度是:maxcharacters','array'=>':attribute的最大数量是:max.',],'mimes'=>':attribute的文件类型必须是:values','min'=>['numeric'=>':attribute的最小长度是:minbits','file'=>'文件的大小:attributeisatleast:minKB','string'=>':attribute的最小长度是:mincharacters','array'=>'The:attributehasatleast:minitems',],'not_in'=>'选中的:attributeisinvalid','numeric'=>':attributemustbeanumber','present'=>':attributefieldmustexist','regex'=>':attributeformatisinvalid','required'=>':属性格式无效','required'=>':attributeisrequired','required_if'=>':attributeisrequiredwhen:otheris:value','required_unless'=>':attributeisrequiredunless:otherisin:valuesIn','required_with'=>'当:values存在时需要:attribute字段','required_with_all'=>'当:values存在时需要:attribute字段','required_without'=>':当:values时需要属性字段doesnotexist','required_without_all'=>':attributefieldisrequiredwhenoneof:valuesexists','same'=>':attributeand:othermustmatch','size'=>['数值'=>':attributemustbe:sizebits','file'=>':attributemustbe:sizeKB','string'=>':attributemustbe:sizebitscharacter','array'=>':attributemustcontain:sizeitem',],'string'=>':attributemustbeastring','timezone'=>':attribute必须是一个有效的时间段。','unique'=>':attribute已经存在','url'=>':attribute无效的格式',/*|--------------------------------------------------------------------|自定义验证语言行|--------------------------------------------------------------------------||在这里,您可以使用|为属性指定自定义验证消息。约定“attribute.rule”来命名行。这使得它很快|为给定的属性规则指定特定的自定义语言行。|*/'custom'=>['attribute-name'=>['rule-name'=>'custom-message',],],/*|---------------------------------------------------------------------|自定义验证属性|----------------------------------------------------------------------||以下语言行用于交换属性占位符|改用对读者更友好的东西,例如电子邮件地址|“电子邮件”。这简直是??帮助ps我们让消息更清晰一点。|*/'attributes'=>[//'name'=>'name',//'age'=>'age',],];from:https://learnku.com/articles/...encapsulationhandler封装处理程序$translation_path定位到刚刚创建的lang目录$translation_locale为多语言目录名,即zh_cnnamespaceApp\handlers;classValidatorextends\Illuminate\Validation\Factory{/****创建实例**@return\Illuminate\Validation\Factory*/publicstaticfunctiongetInstance(){static$validator=null;如果($validator===null){$translation_path=__DIR__.'/../lang';$translation_locale='zh_cn';$translation_file_loader=new\Illuminate\Translation\FileLoader(new\Illuminate\Filesystem\Filesystem,$translation_path);$translator=new\Illuminate\Translation\Translator($translation_file_loader,$translation_locale);$validator=new\Illuminate\Validation\Factory($translator);}返回$验证器;}}参考:http://www.xiaosongit.com/ind...直接使用//访问$data['title']='123';$data['content']='123';//验证$validator=Validator::getInstance()->make($data,['title'=>'required|min:10','content'=>'required|min:10',]);自定义消息提示和定义属性名称//Access$data['title']='123';$data['content']='123';//规则$rules=['title'=>'required|min:10','content'=>'required|min:10',];//自定义消息提示$messages=['title.required'=>':titlefieldmust'];//属性名$attributes=['title'=>'title','content'=>'content',];//验证$validator=Validator::getInstance()->make($data,$rules,$message,$attributes);打印错误信息if($validator->fails()){print_r($validator->errors()->all());}PS:校验规则请前往LaravelValidator文档查看finish!