日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

在非Laravel項(xiàng)目中怎么使用Validator驗(yàn)證器


在非Laravel項(xiàng)目中使用Validator驗(yàn)證器

安裝

composer require illuminate/validation

引入提示消息

項(xiàng)目根目錄創(chuàng)建 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.
|
*/
 
'accepted'             => ':attribute必須接受',
'active_url'           => ':attribute必須是一個(gè)合法的 URL',
'after'                => ':attribute 必須是 :date 之后的一個(gè)日期',
'after_or_equal'       => ':attribute 必須是 :date 之后或相同的一個(gè)日期',
'alpha'                => ':attribute只能包含字母',
'alpha_dash'           => ':attribute只能包含字母、數(shù)字、中劃線或下劃線',
'alpha_num'            => ':attribute只能包含字母和數(shù)字',
'array'                => ':attribute必須是一個(gè)數(shù)組',
'before'               => ':attribute 必須是 :date 之前的一個(gè)日期',
'before_or_equal'      => ':attribute 必須是 :date 之前或相同的一個(gè)日期',
'between'              => [
    'numeric' => ':attribute 必須在 :min 到 :max 之間',
    'file'    => ':attribute 必須在 :min 到 :max KB 之間',
    'string'  => ':attribute 必須在 :min 到 :max 個(gè)字符之間',
    'array'   => ':attribute 必須在 :min 到 :max 項(xiàng)之間',
],
'boolean'              =>':attribute字符必須是 true 或false, 1 或 0 ',
'confirmed'            => ':attribute 二次確認(rèn)不匹配',
'date'                 => ':attribute 必須是一個(gè)合法的日期',
'date_format'          => ':attribute 與給定的格式 :format 不符合',
'different'            => ':attribute 必須不同于 :other',
'digits'               => ':attribute必須是 :digits 位.',
'digits_between'       => ':attribute 必須在 :min 和 :max 位之間',
'dimensions'           => ':attribute具有無(wú)效的圖片尺寸',
'distinct'             => ':attribute字段具有重復(fù)值',
'email'                => ':attribute必須是一個(gè)合法的電子郵件地址',
'exists'               => '選定的 :attribute 是無(wú)效的.',
'file'                 => ':attribute必須是一個(gè)文件',
'filled'               => ':attribute的字段是必填的',
'image'                => ':attribute必須是 jpeg, png, bmp 或者 gif 格式的圖片',
'in'                   => '選定的 :attribute 是無(wú)效的',
'in_array'             => ':attribute 字段不存在于 :other',
'integer'              => ':attribute 必須是個(gè)整數(shù)',
'ip'                   => ':attribute必須是一個(gè)合法的 IP 地址。',
'json'                 => ':attribute必須是一個(gè)合法的 JSON 字符串',
'max'                  => [
    'numeric' => ':attribute 的最大長(zhǎng)度為 :max 位',
    'file'    => ':attribute 的最大為 :max',
    'string'  => ':attribute 的最大長(zhǎng)度為 :max 字符',
    'array'   => ':attribute 的最大個(gè)數(shù)為 :max 個(gè).',
],
'mimes'                => ':attribute 的文件類型必須是 :values',
'min'                  => [
    'numeric' => ':attribute 的最小長(zhǎng)度為 :min 位',
    'file'    => ':attribute 大小至少為 :min KB',
    'string'  => ':attribute 的最小長(zhǎng)度為 :min 字符',
    'array'   => ':attribute 至少有 :min 項(xiàng)',
],
'not_in'               => '選定的 :attribute 是無(wú)效的',
'numeric'              => ':attribute 必須是數(shù)字',
'present'              => ':attribute 字段必須存在',
'regex'                => ':attribute 格式是無(wú)效的',
'required'             => ':attribute 字段是必須的',
'required_if'          => ':attribute 字段是必須的當(dāng) :other 是 :value',
'required_unless'      => ':attribute 字段是必須的,除非 :other 是在 :values 中',
'required_with'        => ':attribute 字段是必須的當(dāng) :values 是存在的',
'required_with_all'    => ':attribute 字段是必須的當(dāng) :values 是存在的',
'required_without'     => ':attribute 字段是必須的當(dāng) :values 是不存在的',
'required_without_all' => ':attribute 字段是必須的當(dāng) 沒有一個(gè) :values 是存在的',
'same'                 => ':attribute和:other必須匹配',
'size'                 => [
    'numeric' => ':attribute 必須是 :size 位',
    'file'    => ':attribute 必須是 :size KB',
    'string'  => ':attribute 必須是 :size 個(gè)字符',
    'array'   => ':attribute 必須包括 :size 項(xiàng)',
],
'string'               => ':attribute 必須是一個(gè)字符串',
'timezone'             => ':attribute 必須是個(gè)有效的時(shí)區(qū).',
'unique'               => ':attribute 已存在',
'url'                  => ':attribute 無(wú)效的格式',
 
/*
|--------------------------------------------------------------------------
| 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' => [
    'attribute-name' => [
        'rule-name' => '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' => [
   // 'name'         => '名字',
   // 'age'         => '年齡',
],
 
];

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


封裝 handler

封裝 handler

1、$translation_path 定位到剛剛創(chuàng)建的 lang 目錄

2、$translation_locale 為多語(yǔ)言目錄名,即 zh_cn

namespace App\handlers;
 
class Validator extends \Illuminate\Validation\Factory
{
    /***
     * 創(chuàng)建實(shí)例
     *
     * @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';
 
// 驗(yàn)證
$validator = Validator::getInstance()->make($data, [
    'title' => 'required|min:10',
    'content' => 'required|min:10',
]);

自定義消息提示和定義屬性名稱

// 接參
$data['title']      =  '123';
$data['content']    = '123';
 
// 規(guī)則
$rules = [
    'title' => 'required|min:10',
    'content' => 'required|min:10',
];
// 自定義消息提示
$messages = [
    'title.required' => ':title字段必須'
];
//屬性名稱
$attributes = [
    'title' => '標(biāo)題',
    'content' => '內(nèi)容',
];
// 驗(yàn)證
$validator = Validator::getInstance()->make($data, $rules, $message, $attributes);

打印錯(cuò)誤消息

if ($validator->fails()) {
    print_r($validator->errors()->all());
}


PS:驗(yàn)證規(guī)則請(qǐng)前往 Laravel Validator 文檔查看


分享到:
標(biāo)簽:Laravel項(xiàng)目 Validator驗(yàn)證器
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定