Laravel 具有 "sometimes" 规则的验证程序自定义消息
Laravel Validator custom message with "sometimes" rule
这是我的一段代码,用于验证表单输入:
public function saveData(Request $request){
$form_data = $request->all();
$validation_fields = [
'first_name' => 'required',
'last_name' => 'required',
'cod_fisc' => 'sometimes|required|size:16',
'p_iva' => 'sometimes|required|between:11,13'
];
$errorMsgs = [
'first_name.required' => 'Il campo Nome è obbligatorio.',
'last_name.required' => 'Il campo Cognome/Ragione sociale è obbligatorio.',
'cod_fisc.required' => 'Il campo Codice Fiscale deve contenere 16 caratteri',
'p_iva.required' => 'Il campo Partita Iva deve contenere 11 o 13 caratteri',
];
$validator = Validator::make($form_data, $validation_fields, $errorMsgs);
....
}
整个项目都是为意大利人编写的,因此所有消息都必须使用意大利语。
一切正常,但绑定到“有时”规则的 cod_fisc
和 p_iva
的两个规则以英文显示。我的自定义错误消息被忽略了。
为什么?
我在搜索您的问题时发现了这个 link:https://laracasts.com/discuss/channels/laravel/sometimes-validator-with-custom-message
其中包含类似的问题。初始代码是
$v = Validator::make(
$request->all(),
[ 'first_name' => 'required|max:60'],
['first_name.required' => 'First name is really required, yo']
);
$v->sometimes('last_name', 'required|in:fake', function($input){
return true;
});
解决方案是
$v = Validator::make(
$request->all(),
[ 'first_name' => 'required|max:60'],
['first_name.required' => 'First name is really required, yo'],
['last_name.in' => 'Last name must be fake, too']
);
$v->sometimes('last_name', 'required|in:fake', function($input){
return true;
});
显然你可以在 Validator::make
的结果上调用 sometimes
function
并传递字段名称、验证器签名和布尔值 function
.
这是我的一段代码,用于验证表单输入:
public function saveData(Request $request){
$form_data = $request->all();
$validation_fields = [
'first_name' => 'required',
'last_name' => 'required',
'cod_fisc' => 'sometimes|required|size:16',
'p_iva' => 'sometimes|required|between:11,13'
];
$errorMsgs = [
'first_name.required' => 'Il campo Nome è obbligatorio.',
'last_name.required' => 'Il campo Cognome/Ragione sociale è obbligatorio.',
'cod_fisc.required' => 'Il campo Codice Fiscale deve contenere 16 caratteri',
'p_iva.required' => 'Il campo Partita Iva deve contenere 11 o 13 caratteri',
];
$validator = Validator::make($form_data, $validation_fields, $errorMsgs);
....
}
整个项目都是为意大利人编写的,因此所有消息都必须使用意大利语。
一切正常,但绑定到“有时”规则的 cod_fisc
和 p_iva
的两个规则以英文显示。我的自定义错误消息被忽略了。
为什么?
我在搜索您的问题时发现了这个 link:https://laracasts.com/discuss/channels/laravel/sometimes-validator-with-custom-message
其中包含类似的问题。初始代码是
$v = Validator::make(
$request->all(),
[ 'first_name' => 'required|max:60'],
['first_name.required' => 'First name is really required, yo']
);
$v->sometimes('last_name', 'required|in:fake', function($input){
return true;
});
解决方案是
$v = Validator::make(
$request->all(),
[ 'first_name' => 'required|max:60'],
['first_name.required' => 'First name is really required, yo'],
['last_name.in' => 'Last name must be fake, too']
);
$v->sometimes('last_name', 'required|in:fake', function($input){
return true;
});
显然你可以在 Validator::make
的结果上调用 sometimes
function
并传递字段名称、验证器签名和布尔值 function
.