登录  /  注册
首页 > php框架 > Laravel > 正文

在非Laravel项目中怎么使用Validator验证器

藏色散人
发布: 2021-02-11 09:20:39
转载
2301人浏览过

下面由laravel教程栏目给大家介绍在非laravel项目中使用validator验证器的方法 ,希望对需要的朋友有所帮助!

在非Laravel项目中怎么使用Validator验证器

安装

composer require illuminate/validation
登录后复制

引入提示消息

项目根目录创建 lang/zh_cn/validation.php 文件

<?php return [

/*
|--------------------------------------------------------------------------
| Validation Language Lines
|--------------------------------------------------------------------------
|
| The following language lines contain the default error messages used by
| the validator class. Some of these rules have multiple versions such
| as the size rules. Feel free to tweak each of these messages here.
|
*/

&#39;accepted&#39;             => ':attribute必须接受',
'active_url'           =&gt; ':attribute必须是一个合法的 URL',
'after'                =&gt; ':attribute 必须是 :date 之后的一个日期',
'after_or_equal'       =&gt; ':attribute 必须是 :date 之后或相同的一个日期',
'alpha'                =&gt; ':attribute只能包含字母',
'alpha_dash'           =&gt; ':attribute只能包含字母、数字、中划线或下划线',
'alpha_num'            =&gt; ':attribute只能包含字母和数字',
'array'                =&gt; ':attribute必须是一个数组',
'before'               =&gt; ':attribute 必须是 :date 之前的一个日期',
'before_or_equal'      =&gt; ':attribute 必须是 :date 之前或相同的一个日期',
'between'              =&gt; [
    'numeric' =&gt; ':attribute 必须在 :min 到 :max 之间',
    'file'    =&gt; ':attribute 必须在 :min 到 :max KB 之间',
    'string'  =&gt; ':attribute 必须在 :min 到 :max 个字符之间',
    'array'   =&gt; ':attribute 必须在 :min 到 :max 项之间',
],
'boolean'              =&gt;':attribute字符必须是 true 或false, 1 或 0 ',
'confirmed'            =&gt; ':attribute 二次确认不匹配',
'date'                 =&gt; ':attribute 必须是一个合法的日期',
'date_format'          =&gt; ':attribute 与给定的格式 :format 不符合',
'different'            =&gt; ':attribute 必须不同于 :other',
'digits'               =&gt; ':attribute必须是 :digits 位.',
'digits_between'       =&gt; ':attribute 必须在 :min 和 :max 位之间',
'dimensions'           =&gt; ':attribute具有无效的图片尺寸',
'distinct'             =&gt; ':attribute字段具有重复值',
'email'                =&gt; ':attribute必须是一个合法的电子邮件地址',
'exists'               =&gt; '选定的 :attribute 是无效的.',
'file'                 =&gt; ':attribute必须是一个文件',
'filled'               =&gt; ':attribute的字段是必填的',
'image'                =&gt; ':attribute必须是 jpeg, png, bmp 或者 gif 格式的图片',
'in'                   =&gt; '选定的 :attribute 是无效的',
'in_array'             =&gt; ':attribute 字段不存在于 :other',
'integer'              =&gt; ':attribute 必须是个整数',
'ip'                   =&gt; ':attribute必须是一个合法的 IP 地址。',
'json'                 =&gt; ':attribute必须是一个合法的 JSON 字符串',
'max'                  =&gt; [
    'numeric' =&gt; ':attribute 的最大长度为 :max 位',
    'file'    =&gt; ':attribute 的最大为 :max',
    'string'  =&gt; ':attribute 的最大长度为 :max 字符',
    'array'   =&gt; ':attribute 的最大个数为 :max 个.',
],
'mimes'                =&gt; ':attribute 的文件类型必须是 :values',
'min'                  =&gt; [
    'numeric' =&gt; ':attribute 的最小长度为 :min 位',
    'file'    =&gt; ':attribute 大小至少为 :min KB',
    'string'  =&gt; ':attribute 的最小长度为 :min 字符',
    'array'   =&gt; ':attribute 至少有 :min 项',
],
'not_in'               =&gt; '选定的 :attribute 是无效的',
'numeric'              =&gt; ':attribute 必须是数字',
'present'              =&gt; ':attribute 字段必须存在',
'regex'                =&gt; ':attribute 格式是无效的',
'required'             =&gt; ':attribute 字段是必须的',
'required_if'          =&gt; ':attribute 字段是必须的当 :other 是 :value',
'required_unless'      =&gt; ':attribute 字段是必须的,除非 :other 是在 :values 中',
'required_with'        =&gt; ':attribute 字段是必须的当 :values 是存在的',
'required_with_all'    =&gt; ':attribute 字段是必须的当 :values 是存在的',
'required_without'     =&gt; ':attribute 字段是必须的当 :values 是不存在的',
'required_without_all' =&gt; ':attribute 字段是必须的当 没有一个 :values 是存在的',
'same'                 =&gt; ':attribute和:other必须匹配',
'size'                 =&gt; [
    'numeric' =&gt; ':attribute 必须是 :size 位',
    'file'    =&gt; ':attribute 必须是 :size KB',
    'string'  =&gt; ':attribute 必须是 :size 个字符',
    'array'   =&gt; ':attribute 必须包括 :size 项',
],
'string'               =&gt; ':attribute 必须是一个字符串',
'timezone'             =&gt; ':attribute 必须是个有效的时区.',
'unique'               =&gt; ':attribute 已存在',
'url'                  =&gt; ':attribute 无效的格式',

/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/

'custom' =&gt; [
    'attribute-name' =&gt; [
        'rule-name' =&gt; 'custom-message',
    ],
],

/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap attribute place-holders
| with something more reader friendly such as E-Mail Address instead
| of "email". This simply helps us make messages a little cleaner.
|
*/

'attributes' =&gt; [
   // 'name'         =&gt; '名字',
   // 'age'         =&gt; '年龄',
],

];
登录后复制

来自:https://learnku.com/articles/5840/validation-validation-in-laravel-returns-chinese-prompt#reply104522

封装 handler

封装 handler

  • $translation_path 定位到刚刚创建的 lang 目录
  • $translation_locale 为多语言目录名,即 zh_cn
namespace App\handlers;


class Validator extends \Illuminate\Validation\Factory
{
    /***
     * 创建实例
     *
     * @return \Illuminate\Validation\Factory
     */

    public static function getInstance()
    {
        static $validator = null;
        if ($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);
        }
        return $validator;
    }
}
登录后复制

参考:http://www.xiaosongit.com/index/detail/id/897.html

使用

直接使用

// 接参
$data['title']      =  '123';
$data['content']    = '123';

// 验证
$validator = Validator::getInstance()-&gt;make($data, [
    'title' =&gt; 'required|min:10',
    'content' =&gt; 'required|min:10',
]);
登录后复制

自定义消息提示和定义属性名称

// 接参
$data['title']      =  '123';
$data['content']    = '123';

// 规则
$rules = [
    'title' =&gt; 'required|min:10',
    'content' =&gt; 'required|min:10',
];
// 自定义消息提示
$messages = [
    'title.required' =&gt; ':title字段必须'
];
//属性名称
$attributes = [
    'title' =&gt; '标题',
    'content' =&gt; '内容',
];
// 验证
$validator = Validator::getInstance()-&gt;make($data, $rules, $message, $attributes);
登录后复制

打印错误消息

if ($validator-&gt;fails()) {
    print_r($validator-&gt;errors()-&gt;all());
}
登录后复制

PS:验证规则请前往 Laravel Validator 文档查看

finish!

以上就是在非Laravel项目中怎么使用Validator验证器的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:segmentfault网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号