Php Laravel 5中的表单验证消息与语言
阅读完文档后,我理解了Php Laravel 5中的表单验证消息与语言,php,validation,laravel-5,Php,Validation,Laravel 5,阅读完文档后,我理解了resources/lang/xx/validation.php中添加自定义错误消息的结构 'custom' => [ 'name' => [ 'required' => 'Please fill the name of the company' ] ], 我这里的问题是,在设置此项后,我将无法将名为名称的字段用于其他资源,例如请填写您的姓名消息。当在FormRequest中重写方法messages()时,我可以在这些消息
resources/lang/xx/validation.php
中添加自定义错误消息的结构
'custom' => [
'name' => [
'required' => 'Please fill the name of the company'
]
],
我这里的问题是,在设置此项后,我将无法将名为名称
的字段用于其他资源,例如请填写您的姓名
消息。当在FormRequest中重写方法messages()
时,我可以在这些消息中更加具体,但随后我会丢失语言
自定义
我的问题是:如何为不同的表单请求设置自定义消息,同时保持语言系统对我有利
编辑
为了更好地解释我想要什么,这里有一些代码
class CompanyRequest extends Request {
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules() {
return [
'name' => 'required|max:20'
];
}
public function messages() {
// Here I have specific message for Company name field, but I'm
// stuck with one single language.
return [
'name.required' => 'Can you please fill the company name.',
'name.max' => 'The name of the company cannot be bigger than 20 characters'
];
}
}
现在是用户请求
class UserRequest extends Request {
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules() {
return [
'name' => 'required|max:40',
'email' => 'required|email'
];
}
public function messages() {
// Here I have specific message for User name field, but I'm stuck
// with one single language.
return [
'name.required' => 'Please provide your full name',
'name.max' => 'If your name is bigger than 40 characters, consider using only first and last name.'
];
}
}
现在,这很好用,但我还是坚持用英语。我可以通过resources/lang/en/validation.php
文件来解决这个问题。现在,我将删除messages
方法并使用验证文件
'custom' => [
// Here I can have multiple validation files for different languages,
// but how would I go about to define the field name for other entities
// (such as the user message).
'name' => [
'required' => 'Please fill the name of the company',
'max' => 'The field cannot have more than 20 characters',
]
]
现在,如何区分公司实体的name
字段和用户实体的name
字段的消息
总结一下:
- 表单请求为我提供了一种覆盖messages()方法的方法,该方法允许我对多个表单使用术语
name
- 验证文件为我提供了一种使用多种语言的方法(我可以创建
,但是我只能定义一次字段“name”。对于一个字段名,我不能有不同的消息resources/lang/fr/Validation.php
resources/lang/en/validation.php
将包含您的英语消息和resources/lang/es/validation.php
将保留您的西班牙语作为相同的格式,如果您的问题是正确的要在包含与表单相同字段名的不同表单上定义lang,您需要查看此repo:我不确定我是否理解您的意思您的资源/land/xx。文件夹应包含验证消息的翻译。您可以更具体一点,或者提供一个示例,例如英语和西班牙语吗scenario@futureweb我更新了我的问题,试图进一步澄清我的问题。我想这可能会对@futureweb有所帮助。我的目的不是为每种语言定义不同的字段名,而是为同一字段名定义不同的消息验证。那么你所做的是对的?resources/lang/en/validation.php
将包含你的英语消息s和resources/lang/es/validation.php
如果您的问题是在不同的表单上定义lang,该表单包含与您需要查看此repo的表单相同的字段名,则将保留相同表单的西班牙语: