Laravel-validation-錯誤資訊--中文 阿新 • • 發佈:2019-01-13 1.複製resources/lang/en 資料夾改為zh 2.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 是不可用的連結.', 'after' => '注意: :attribute 必須為一個在 :date 之後的時間.', 'alpha' => '注意: :attribute 只允許全部為字母.', 'alpha_dash' => '注意: :attribute 只允許字幕,數字和_連線符.', 'alpha_num' => '注意: :attribute 只允許字幕和數字.', 'array' => '注意: :attribute 必須為陣列.', 'before' => '注意: :attribute 必須為一個在 :date 之前的時間.', 'between' => [ 'numeric' => '注意: :attribute 必須在 :min 至 :max 之間.', 'file' => '注意: :attribute 必須限制在 :min 至 :max k.', 'string' => '注意: :attribute 必須在 :min 至 :max 字元.', 'array' => '注意: :attribute 必須包含 :min 至 :max 鍵.', ], 'boolean' => '注意: :attribute 欄位必須為 true 或 false.', 'confirmed' => '注意: :attribute 確認不匹配.', 'date' => '注意: :attribute 是一個不可用的時間.', 'date_format' => '注意: :attribute 不匹配: format :format.', 'different' => '注意: :attribute 和 :other 必須不同.', 'digits' => '注意: :attribute 必須 :digits 數值.', 'digits_between' => '注意: :attribute 必須在 :min 至 :max 數值之間.', 'dimensions' => '注意: :attribute 有無效的圖片尺寸.', 'distinct' => '注意: :attribute 欄位具有重複的值.', 'email' => '注意: :attribute 必須為可用的郵箱地址.', 'exists' => '注意: 選中的 :attribute 不可用.', 'file' => '注意: :attribute 必須為一個檔案.', 'filled' => '注意: :attribute 欄位必填.', 'image' => '注意: :attribute 必須為圖片.', 'in' => '注意: 選中的 :attribute 不可以.', 'in_array' => '注意: :attribute 欄位未出現在 :other 其中.', 'integer' => '注意: :attribute 必須為整數.', 'ip' => '注意: :attribute 必須為合法的IP地址.', 'json' => '注意: :attribute 必須為合法的JSON格式資料.', 'max' => [ 'numeric' => '注意: :attribute 不可以比 :max 大.', 'file' => '注意: :attribute 不能超過 :max 千位元組.', 'string' => '注意: :attribute 不可超過 :max 個字元.', 'array' => '注意: :attribute 不能超過 :max 個鍵值對.', ], 'mimes' => '注意: :attribute 必須為: :values 型別的檔案.', 'min' => [ 'numeric' => '注意: :attribute 必須最小為 :min.', 'file' => '注意: :attribute 必須至少 :min 千位元組.', 'string' => '注意: :attribute 必須至少 :min 字元.', 'array' => '注意: :attribute 必須至少包含 :min 個鍵值對.', ], 'not_in' => '注意: 選中的 :attribute 不合法.', 'numeric' => '注意: :attribute 必須為數字.', 'present' => '注意: :attribute 必須出現.', 'regex' => '注意: :attribute 格式不合規範.', 'required' => '注意: :attribute 欄位必填.', 'required_if' => '注意: :attribute 欄位必填當 :other 即 :value.', 'required_unless' => '注意: :attribute 必填除非 :other 在 :values 其中.', 'required_with' => '注意: :attribute 必填當 :values 出現.', 'required_with_all' => '注意: :attribute 欄位必填當 :values 出現.', 'required_without' => '注意: :attribute 欄位必填當 :values 沒有出現.', 'required_without_all' => '注意: :attribute 欄位必填當 :values 無一可用.', 'same' => '注意: :attribute 和 :other 必須保持一致.', 'size' => [ 'numeric' => '注意: :attribute 必須 :size.', 'file' => '注意: :attribute 必須包含 :size 千位元組.', 'string' => '注意: :attribute 必須包含 :size 字元.', 'array' => '注意: :attribute 必須包含 :size 鍵.', ], 'string' => '注意: :attribute 必須為字元.', 'timezone' => '注意: :attribute 時區必須為合理的時區.', 'unique' => '注意: :attribute 已經被佔用,請更換.', 'url' => '注意: :attribute 格式不可用.', 'zh_mobile' => '注意: :attribute 不是有效的電話號碼.', /* |-------------------------------------------------------------------------- | Custom Validation Language Lines |-------------------------------------------------------------------------- | | Here you may specify custom validation messages for attributes using 注意: | convention "attribute.rule" to name 注意: 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 |-------------------------------------------------------------------------- | | 注意: 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' => [ ], 'identitycards' => '身份證號錯誤!', ]; 3.修改config\app.php 'locale' => 'zh', 4.新建錯誤資訊程式碼檔案(直接右鍵建立): ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20190103162919276.png) 5.複製程式碼到錯誤程式碼檔案 <?php namespace App\Exceptions; class ExceptionErrorCode{ const OPERATOR_OK = 0; const OPERATOR_ERROR=13; const IMAGE_DELETE_ERROR=15; const VALIDATE_FAILD = 422; const MODEL_DATA_NOT_EXISTS = 100000; const DATA_CREATE_FAILED = 100001; const DATA_UPDATE_FAILED = 100002; const DATA_DELETE_FAILED = 100003; const SERVICE_DATA_ERROR = 100004; const DATABASE_ERROR = 100005; const ONLY_ONE_INFORMATION_CAN_BE_SUBMITTED = 100006; const UNIQUE_DATA_ALEARDY_EXISTS = 100006; const REQUEST_ARGUEMENT_DEFICIENFY = 200001; const REQUEST_ARGUEMENT_ERROR = 200002; const UPLOAD_FILE_NOT_EXISTS = 200003; const UPLOAD_FILE_NOT_VALID = 200004; const NO_STORE_PERMISSIONS = 200005; const NOT_SUFFICIENT_FUND = 300000; const CURRENT_ACCOUNT_IS_AREADY_BIND_PLATFORM = 300001; const PLATFORM_ACCOUNT_STORE_FAILD = 300002; const PLATFORM_FUND_LIST_CREATE_FAILD = 300002; const PLATFORM_FUND_CHANGE_RECORD_FAILD = 300003; const ACCOUNT_EXISTS_FUND_CHANGE_RECORD = 300004; const CREDIT_LINE = 300005; const ACTIVATION_FAILURE = 300006; const COURSE_ALEARDY_SHARED = 400000; const SHARING_COURSE_PLATFORM_NOT_FOUND = 400001; const SHARING_COURSE_NOT_EXISTS = 400002; const CURRENT_RESOURCE_NOT_SHARING = 400003; const COURSE_NOT_SHARED = 400004; const CAN_NOT_RETAIL_SELF_SHARING_RESOURCE = 400005; const PARENT_COURSE_CATEGORY_NOT_EXISTS = 400006; const PARENT_COURSE_CATEGORY_CAN_NOT_BE_SELF_CHILD = 400007; const CURRENT_COURSE_CATEGORY_IS_USING_BY_RESOURCES = 400008; const RETAIL_COURSE_NOT_EXISTS = 400009; const LEARNING_PLAN_COPY_FAILD = 400010; const LEARNING_PLAN_COURSE_COPY_FAILD = 400011; const RESOURCE_END_DATE_GT_THAN_OPEN_DATE = 400012; const LEARNING_PAN_NOT_HAS_COURSE_RELATION = 400013; const SHARING_RESOURCE_WAIT_AUDIT = 400014; const NO_PUBLISHED_RESOURCE_CAN_NOT_SHARING = 400015; const COPY_RESOURCE_NOT_FOUND = 400016; const CURRENT_SHARING_IS_CLOSED = 400017; const CURRENT_SHARING_WAIT_APPLYFOR = 400018; const CURRENT_IS_NOT_CHANGE_APPLYFOR_STATUS = 400019; const CURRENT_IS_NOT_CLOSED_APPLYFOR_STATUS = 400020; const CURRENT_SHARING_WAIT_CLOSE_DISPOSE = 400021; const CURRENT_SHARING_WAIT_CHANGE_DISPOSE = 400022; const CURRENT_LEARNING_PLAN_IS_SHARING = 400023; const COURSE_CATEGORY_NOT_EXISTS = 400024; const RETAIL_COPY_RECORD_FAILD = 400025; const UPDATE_SET_RETAIL_STATUS_AND_RESOURCE_NEW_ID_FAILD = 400026; const RETAIL_COPY_RECORD_NOT_FOUND = 400027; const DECLINE_SHARING_APPLYFOR_BRIEF_NOT_SET = 400028; const RETAIL_RESOURCE_DO_NOT_SHARING = 400029; const OPERATOR_EXPLAIN_NOT_GIVEN = 500000; const SMS_CODE_SEND_FAILD = 600000; const NUKNOWNERROR = 10000; const CODEMUST = 20000; const CODEMISTAKE = 20001; const CODEOVERDUE = 20002; const ERROR_USER_NOT_FOUND = 20003; const ERROR_USER_FIELD_MORE_ATTEMPTS = 20004; const EXITERROR = 20005; const UNLISTEDWORD = 20006; const USEREXIST = 20007; const TOKENPASTDUE = 20008; const TOKENLACK = 20009; const TOKENLOSE_EFFICACY = 20010; const There_Is_an_Activity_That_Cannot_Be_Deleted = 20011; const THIS_ACTIVITY_TYPE_DOES_NOT_EXIST = 20012; const THIS_ACTIVITY_DOES_NOT_EXIST = 20013; const IMAGE_FORMAT_IS_NOT_CORRECT = 20014; const IMAGE_FORMAT_EFFICACY = 20019; const IMAGE_LACK = 20015; const AD_REPOSOTPRYS_NOT_CORRECT = 20016; const AD_GROUP_CORRECT = 20017; const AD_GROUP_SUBPLATFORM_RELATION_CORRECT = 20018; const AD_CREDIT_CARD_EXISTS = 20019; const AD_OPERATION_EXECUTION= 20020; const NO_USER_SHOP_INFORMATION= 20021; const INFORMATION_IS_BEING_AUDITED= 20022; const INFORMATION_REVIEW_APPROVAL= 20023; const SUPPLEMENTARY_INFORMATION= 20024; const INFORMATION_TO_BE_AUDITED= 20025; const CORRECT_OPERATION = 20026; const ROLE_EXISTS_USER = 20027; const NOT_ROLE_EXISTS_USER = 20028; const APPLICANT_MERCHANT = 20029; const INFORMATION_DOES_NOT_EXIST_OR_IS_WRONG = 20030; const PASSWORD_NOT = 20031; const ACTIVATION_NOT = 20032; const COMMODITY_COLLECTION_EXISTENCE = 20033; const COMMODITY_COLLECTION = 20034; const PRODUCT_NOT = 20035; const PASS_NOT_OLD = 20036; const PASS_NOT_OLDS = 20037; const REPEAR_SUBMISSION = 20038; const LACK_OF_STOCK = 20039; const NEW_NOT_PHONE = 20040; const ADDRESS_NOT = 20041; const HAVE_BEEN_APPLIED_FOR_AFTER_SALE_DO_NOT_SUBMIT_REPEATEDLY = 20042; const ORDER_TRANSACTION_NOT_COMPLETED = 20043; const IMAGE_LACK_TOO_MUCH = 20044; const FOLLOW_SHOP_NOT = 20045; const RECEIVING_ASSRESS = 20046; const ORDER_NO = 20046; const ORDER_NOT = 20047; const ORDER_INCORRECT_OPERATION = 20048; const RED_NOT = 20049; const GOODS_NOT = 20050; const UNPAID = 20051; const SET_PAYMENT_PASSWORD = 20052; const PAYMENT_PASSWORD_NOT = 20053; const NOT_ENOUGH = 20054; //簡訊錯誤碼 const SEND_A_TEXT_MESSAGE_SUCCESSFULLY = 80001; const MOBILE_WRONG=80013; const SIGNATURE_FORMAT_ERROR = 80015; const SIGNATURE_DISABLE = 80016; const CONTAINER = [ self::IMAGE_DELETE_ERROR => '圖片刪除失敗', self::OPERATOR_ERROR => '操作失敗', self::OPERATOR_OK => '操作成功', self::VALIDATE_FAILD => '資料驗證失敗', self::MODEL_DATA_NOT_EXISTS => '請求的資料不存在', self::DATA_CREATE_FAILED => '資料建立失敗', self::DATA_UPDATE_FAILED => '資料更新失敗', self::DATA_DELETE_FAILED => '資料刪除失敗', self::SERVICE_DATA_ERROR => '伺服器資料異常', self::REQUEST_ARGUEMENT_DEFICIENFY => '請求引數不足', self::REQUEST_ARGUEMENT_ERROR => '請求引數錯誤', self::NOT_SUFFICIENT_FUND => '餘額不足', self::DATABASE_ERROR => '資料庫異常', self::ONLY_ONE_INFORMATION_CAN_BE_SUBMITTED => '您已提交過資料,請等待稽核結果,別重複提交', self::OPERATOR_EXPLAIN_NOT_GIVEN=> '處理行為請作出說明', self::SMS_CODE_SEND_FAILD => '簡訊驗證碼傳送失敗', self::CREDIT_LINE => '信用額度不足', self::ACTIVATION_FAILURE => '已啟用,請勿重複啟用', self::NO_STORE_PERMISSIONS =>'你沒有操作許可權', self::CODEMUST => '驗證碼必填', self::CODEMISTAKE => '驗證碼錯誤', self::CODEOVERDUE => '驗證碼過期', self::ERROR_USER_NOT_FOUND => '該使用者不存在或密碼錯誤', self::ERROR_USER_FIELD_MORE_ATTEMPTS => '請求次數過多,請稍後再試', self::EXITERROR => '退出登入失敗', self::UNLISTEDWORD => '未登入', self::NUKNOWNERROR => '未知錯誤', self::USEREXIST => '該使用者名稱已存在', self::TOKENPASTDUE => 'token已過期,請重新登入', self::TOKENLACK =>'缺少token', self::TOKENLOSE_EFFICACY => 'token失效', self::IMAGE_FORMAT_IS_NOT_CORRECT => '圖片格式不正確', self::IMAGE_FORMAT_EFFICACY => '圖片格式無效', self::IMAGE_LACK => '需要一張圖片', self::AD_REPOSOTPRYS_NOT_CORRECT => '廣告資源不存在', self::AD_GROUP_CORRECT => '該名稱已存在', self::AD_GROUP_SUBPLATFORM_RELATION_CORRECT => '該名稱已經新增請勿重複新增', self::AD_CREDIT_CARD_EXISTS => '存在該類信用卡,不能刪除', self::AD_OPERATION_EXECUTION => '已經稽核請勿重複稽核', self::NO_USER_SHOP_INFORMATION => '沒有該使用者店鋪資訊', self::INFORMATION_IS_BEING_AUDITED => '資訊正在稽核中', self::INFORMATION_REVIEW_APPROVAL => '資訊稽核通過', self::SUPPLEMENTARY_INFORMATION => '請補充資料', self::INFORMATION_TO_BE_AUDITED => '資訊待稽核', self::CORRECT_OPERATION => '請執行正確操作', self::ROLE_EXISTS_USER => '該角色下存在使用者,請先刪除使用者', self::NOT_ROLE_EXISTS_USER => '該使用者已經繫結角色,請勿重複繫結導致許可權衝突', self::APPLICANT_MERCHANT=>'請先申請成為商戶', self::PRODUCT_NOT=>'該商品不存在', self::INFORMATION_DOES_NOT_EXIST_OR_IS_WRONG=>'資訊輸出有誤,請重新輸入', self::PASSWORD_NOT=>'兩次密碼輸入不一致', self::ACTIVATION_NOT=>'請啟用後登入', self::PASS_NOT_OLD=>'與原密碼不匹配', self::PASS_NOT_OLDS=>'請勿重複啟用', self::REPEAR_SUBMISSION=>'請勿重複提交資料', self::GOODS_NOT=>'商品不存在', self::NEW_NOT_PHONE=>'請繫結新手機號碼', self::ADDRESS_NOT=>'地址不存在', self::HAVE_BEEN_APPLIED_FOR_AFTER_SALE_DO_NOT_SUBMIT_REPEATEDLY=>'已申請售後,不重複提交', self::ORDER_TRANSACTION_NOT_COMPLETED=>'訂單未完成', self::IMAGE_LACK_TOO_MUCH=>'上傳圖片超上限', self::ORDER_NO=>'無此訂單', self::ORDER_NOT=>'訂單支付失敗', self::ORDER_INCORRECT_OPERATION=>'該訂單未付款', self::RED_NOT=>'紅包不存在', self::UNPAID=>'未支付', self::SET_PAYMENT_PASSWORD=>'請設定支付密碼', self::PAYMENT_PASSWORD_NOT=>'支付密碼不正確', self::NOT_ENOUGH=>'商品數量不足請重新選擇', self::COMMODITY_COLLECTION_EXISTENCE=>'該商品已經存在於您的收藏夾中。', self::COMMODITY_COLLECTION=>'收藏成功。', self::LACK_OF_STOCK=>'商品庫存不足', self::FOLLOW_SHOP_NOT=>'你已經關注過該店鋪', self::RECEIVING_ASSRESS=>'新增收貨地址達到上限', self::SEND_A_TEXT_MESSAGE_SUCCESSFULLY=>'傳送簡訊成功', self::MOBILE_WRONG=>'手機號碼錯誤', self::SIGNATURE_FORMAT_ERROR=>'簽名不合規', self::SIGNATURE_DISABLE=>'簽名遮蔽', ]; } 6.控制器內使用: 例如 use App\Exceptions\ExceptionErrorCode; use Illuminate\Support\Facades\Validator; public function delQr(Request $request){ $valid = Validator::make($request->all(), [ 'id_arr' => 'required|array', // 'batches'=> 'required|string' ]); if ($valid->fails()) { return array( 'code' => ExceptionErrorCode::CODEMUST, 'message' => $valid->errors()->first(), 'data' => [] ); } }