Laravel 中 validation 验证 返回中文提示 全局设置

首先一个简单的验证的例子

use Illuminate\Support\Facades\Validator;

 $validator = Validator::make(request()->all(),
['username.required'=>'请填写用户名'],
['password.required'=>'请填写密码']
);
if ($validator->fails()) {
return $validator->errors()->first();
}

自己建一个zn文件夹,然后把en的4个文件全复制过去,修改validation.php的代码为下面的内容,然后在app.php修改成你的文件夹

文章地址 https://laravel-china.org/articles/5840/validation-validation-in-laravel-returns-chinese-prompt

更改 resources\lang 目录下边的 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必须是一个合法的 URL',
'after' => ':attribute 必须是 :date 之后的一个日期',
'after_or_equal' => ':attribute 必须是 :date 之后或相同的一个日期',
'alpha' => ':attribute只能包含字母',
'alpha_dash' => ':attribute只能包含字母、数字、中划线或下划线',
'alpha_num' => ':attribute只能包含字母和数字',
'array' => ':attribute必须是一个数组',
'before' => ':attribute 必须是 :date 之前的一个日期',
'before_or_equal' => ':attribute 必须是 :date 之前或相同的一个日期',
'between' => [
'numeric' => ':attribute 必须在 :min 到 :max 之间',
'file' => ':attribute 必须在 :min 到 :max KB 之间',
'string' => ':attribute 必须在 :min 到 :max 个字符之间',
'array' => ':attribute 必须在 :min 到 :max 项之间',
],
'boolean' => ':attribute 字符必须是 true 或 false',
'confirmed' => ':attribute 二次确认不匹配',
'date' => ':attribute 必须是一个合法的日期',
'date_format' => ':attribute 与给定的格式 :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必须是 jpeg, png, bmp 或者 gif 格式的图片',
'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 KB',
'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 KB',
'string' => ':attribute 必须是 :size 个字符',
'array' => ':attribute 必须包括 :size 项',
],
'string' => ':attribute 必须是一个字符串',
'timezone' => ':attribute 必须是个有效的时区.',
'unique' => ':attribute 已存在',
'url' => ':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' => [
'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' => '年龄',
], ];

laravel 验证机制validation的更多相关文章

  1. ASP.NET MVC 学习7、为Model Class的字段添加验证属性(validation attribuate)

    Adding Validation to the Model ,在Model中添加数据验证 参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-star ...

  2. 定制Asp.NET 5 MVC内建身份验证机制 - 基于自建SQL Server用户/角色数据表的表单身份验证

    背景 在需要进行表单认证的Asp.NET 5 MVC项目被创建后,往往需要根据项目的实际需求做一系列的工作对MVC 5内建的身份验证机制(Asp.NET Identity)进行扩展和定制: Asp.N ...

  3. 通过扩展改善ASP.NET MVC的验证机制[使用篇]

    原文:通过扩展改善ASP.NET MVC的验证机制[使用篇] ASP.NET MVC提供一种基于元数据的验证方式是我们可以将相应的验证特性应用到作为Model实体的类型或者属性/字段上,但是这依然具有 ...

  4. 理解和使用WPF 验证机制

    博客 学院 下载 更多 写博客 发布Chat 登录注册 理解和使用WPF 验证机制 原创 2013年06月20日 11:15:37 7404 首先建立一个demo用以学习和实验WPF Data Val ...

  5. 【MVC】ASP.NET MVC Forms验证机制

    http://www.cnblogs.com/bomo/p/3309766.html 随笔 - 121  文章 - 0  评论 - 92 [MVC]ASP.NET MVC Forms验证机制 ASP. ...

  6. jquery.validate 验证机制

    jquery.validate 验证机制 金刚 juqery juqery.validate 在开发系统时,使用了jquery.validate.js 这个验证插件,来校验数据合法性 重点 验证是以i ...

  7. struts2 的验证框架validation如何返回json数据 以方便ajax交互

    struts2 的验证框架validation简单,好用,但是input只能输出到jsp页面通过struts2的标签<s:fielderror  />才能取出,(EL应该也可以). 如果使 ...

  8. 【jQuery基础学习】06 jQuery表单验证插件-Validation

    jQuery的基础部分前面都讲完了,那么就看插件了. 关于jQuery表单验证插件-Validation validation特点: 内置验证规则:拥有必填.数字.E-Mail.URL和信用卡号码等1 ...

  9. Linux登录验证机制、SSH Bruteforce Login学习

    相关学习资料 http://files.cnblogs.com/LittleHann/linux%E4%B8%AD%E7%94%A8%E6%88%B7%E7%99%BB%E5%BD%95%E8%AE% ...

随机推荐

  1. 命令行工具jdb调试Java程序

    一直在gdb 下调试c/c++程序. 突然要写个java程序,又不是很喜欢使用eclipse,那怎么调试程序呢.你还可以使用 jdb. 它是jdk 内置的一个java debug工具.类似与 gdb. ...

  2. 修改maven仓库位置

    在eclipse中安装好maven2的插件后: 第一步: 默认会放在~/.m2/repository目录下 (“~”代表用户的目录,比如windows7下一般都是C:\Users\zz\.m2\rep ...

  3. Ubuntu命令:sudo、shutdown、apt-get、vim

    切换成ROOT用户: Ubuntu中默认不开启root账户,所以root账户是没有密码的, 但是会有一个非root的管理员账户,可以通过sudo来获得root权限,现在就可以用这个账户来设置密码 ** ...

  4. 关于Hibernate在反向工程时无法选择Spring DAO Type的解决方法【更新版】

    目录(?)[+] IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结! 之前有一篇文章中(Hibernate反向工程步骤及DAO Type无法选择Spring DAO解决方法)提到, ...

  5. LA3890 Most Distant Point from the Sea

    题意 PDF 分析 可以二分答案,检验就用半平面交,如果平面非空则合法. 时间复杂度\(O(T n \log^2 n)\) 代码 #include<iostream> #include&l ...

  6. 向HDFS中追加内容

    向生成好的hdfs文件中追加内容,但是线上使用的版本是1.0.3,查看官方文档发现,在1.0.4版本以后才支持文件append 以下是向hdfs中追加信息的操作方法 如果你只在某一个driver中追加 ...

  7. React组件性能优化总结

    性能优化的思路 影响网页性能最大的因素是浏览器的重排(repaint)和重绘(reflow). React的Virtual DOM就是尽可能地减少浏览器的重排和重绘. 从React渲染过程来看,如何防 ...

  8. windows-x64下安装python3.6

    1.下载python3安装包,注意要安装windowsx64的exe安装包.python-3.6.2-amd64.exe 2.设置window的环境变量:C:\Users\您设置的windows用户名 ...

  9. ejs的简单应用

    获取数据 var init = function(){ api.my_bean_list({ },function(datas){ console.log(datas); if (datas.errn ...

  10. PHP中的精确计算bcadd,bcsub,bcmul,bcdiv

    引言:一定要确保数据的准确性.这是一个好的程序员的基本素养. <?php /** * 精确加法 * @param [type] $a [description] * @param [type] ...