php - Lang 文件上的 Laravel 验证
问题描述
我正在使用 laravel 5.8,并且在我的 resource/lang/xx/ 文件夹中包含了一个validation.php。
在我的控制器中,我使用此代码来验证字段:
$validator = Validator::make($request->all(), [
'lastname' => 'required',
'firstname' => 'required',
'gender' => 'required',
'birthplace_id' => 'required',
'birthdate' => 'required',
'fiscal_code' => 'required|unique:customers,fiscal_code,NULL,id,deleted_at,NULL',
]);
我不需要返回 $validator,但我需要在同一个函数中打印错误。我写了这段代码:
if ($validator->fails()) {
\Log::info(json_encode($validator));
}
我得到了这个:
{
"customMessages": [],
"fallbackMessages": [],
"customAttributes": [],
"customValues": [],
"extensions": [],
"replacers": []
}
这是我的validation.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.
|
*/
'required' => 'The :attribute field is required.',
'unique' => 'Il :attribute è già stato utilizzato.',
/*
|--------------------------------------------------------------------------
| 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 our attribute placeholder
| with something more reader friendly such as "E-Mail Address" instead
| of "email". This simply helps us make our message more expressive.
|
*/
'attributes' => [],
];
如何将我的验证器连接到 validation.php 语言文件?
解决方案
它应该自动连接到 validation.php 文件
尝试以这种方式使用它:
$request->validate([
'lastname' => 'required',
'firstname' => 'required',
'gender' => 'required',
'birthplace_id' => 'required',
'birthdate' => 'required',
'fiscal_code' => 'required|unique:customers,fiscal_code,NULL,id,deleted_at,NULL'
]);
推荐阅读
- php - 我如何在 PHP 中的文档上的谷歌表上创建权限
- grails - Grails 2.4.4 - 如何在战争阶段存储配置变量并将其放入 Tomcat 环境中?
- swift - NoMatchingElementException 的 Earlgrey 错误
- c++ - 如何使用单个 c++ 类成员处理不同的结构?
- angular - Angular 5 JWT 总是说“无效令牌”
- angular - Angular 路由的条件
- java - 使用 intellij 和 Tomcat 的 SOAP Web 服务中的 web.xml 错误
- javascript - 如何使麦克风静音(但不是传入声音)?
- java - 在Java中的Hashmap中使用过去的数据和ArrayList的当前数据
- javascript - 当我们点击 iPhoneX 中的文本字段时,键盘没有打开