Laravel-validation-错误信息--中文

解阳荣
2023-12-01

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' => [] ); } }
 类似资料: