gpt4 book ai didi

php - 数组输入的自定义错误消息

转载 作者:搜寻专家 更新时间:2023-10-31 21:03:55 25 4
gpt4 key购买 nike

考虑以下 <input>我表单中的数组:

 <input type="text" name="title[1]" value="">
<input type="text" name="title[2]" value="">
<input type="text" name="title[3]" value="">

数字 (1,2,3) 代表不同的语言。 1 = 英语,2 = 德语,等等。

如何为输入数组添加自定义错误消息?


我在我的 app/lang/en/validation.php 中尝试了以下但没有成功:

<?php   
return [
'custom' => [
'title.1' => [
'required' => 'The english title is required.',
],
'title.2' => [
'required' => 'The german title is required.',
],
'title.3' => [
'required' => 'The italian title is required.',
],
],
];
?>

Laravel 抛出默认错误信息,而不是使用我的自定义信息:

The title.1 field is required.
The title.2 field is required.
The title.3 field is required.

感谢您提供的任何帮助!


编辑:如果我像这样将消息传递给我的验证器,它会起作用:

$messages = array(
'title.1.required' => 'The english title is required',
);
$validator = Validator::make($data = Input::all(), $rules, $messages);

但我无法让它在 app/lang/en/validation.php 中工作文件。

最佳答案

通过使用下面的方法我得到了解决方案

In Controller

        $input=array (
'name' => 'pro 1',
'barcode' => '2222',
'vendors' =>
array (
0 =>
array (
'id' => 51,
'name' => 'v1',
'item_code' => 'khgjhgjhkhjgjhgjhkhjgjhgjhkhjgjhgjhvv',
),
1 =>
array (
'id' => 43,
'name' => 'v3',
'item_code' => 'aerfaf132aw1d32aw1d32wad',
),
),
)
$validation = Product::validate($input);
if ($validation != null && $validation != "" && $validation->fails()) {
$breakline = $validation->messages()->all();
$message = implode("<br> ", $breakline);
Log::warning('Admin::ProductsController::store::' . $message);
return Response()->json('', $message));
}

In Model

public static function validate($data) {
$rule = array(
'name' => 'required|max:255',
'barcode' => 'required|max:255',
'vendors'=>'present|array|size:1,5',
'vendors.*.item_code' => 'max:6'
);
$messages = array(
'required' => ':attribute field is required.',
'name.max' => ':attribute may not be greater than :max characters.',
'barcode.max'=>':attribute may not be greater than :max characters.'
'size'=>'only allowed one to five vendor.',
);
$data = Validator::make($data, $rule, $messages);
$data->setAttributeNames(array(
'name' => ucfirst('name'),
'barcode' => ucfirst('barcode'),
'vendors.*.item_code' => ucfirst('item code'),
));
return $data;
}

It will show

Item code may not be greater than 6.
Item code may not be greater than 6.

关于php - 数组输入的自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35887853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com