阅读完文档后,我已了解resources/lang/xx/validation.php
中的结构,以添加自定义错误消息。
'custom' => [
'name' => [
'required' => 'Please fill the name of the company'
]
],
我的问题是,在设置之后,我无法将名为name
的字段用于其他资源,例如Please fill out your name
消息。覆盖FormRequest中的方法messages()
时,我可以在这些消息中更具体,但后来我失去了Language
自定义。
我的问题是:如何为不同的表单请求设置自定义消息,并保持语言系统对我有利?
为了更好地解释我想要的东西,这里有一些代码。
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'
];
}
}
现在是UserRequest
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
?
包装起来:
name
用于多个表单。resources/lang/fr/validation.php
,但我只能定义字段" name"一次。我不能为字段设置不同的消息名称