Laravel 的 Csv 导入验证不断验证值是否存在
Csv import validation for Laravel constantly validating value as not present
我正在使用 Laravel maatwebsite 3.1.3 行验证来验证 csv 文件导入。但是我的 Imports php 文件中的 WithValidation
问题不断抛出一个错误,即第二个导入行的最后一个值是必需的,即使该值存在也是如此。
当我删除验证时,导入工作正常。
我的控制器调用导入函数:
public function doImport(Request $request) {
$this->validate($request, [
'import_file' => 'required|mimes:xls,csv,xlsx,txt' // txt is needed for csv mime type validation
]);
if($request->file('import_file')) {
try {
Excel::import(new FPImport, $request->file('import_file'));
return redirect('admin/fertil/products')->with('success', 'Invoer Sukses');
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
$msg = '';
$failures = $e->failures();
foreach ($failures as $failure) {
$msg = 'Ry '.$failure->row(); // row that went wrong
$msg = $msg.' vir hoof '.$failure->attribute(); // either heading key (if using heading row concern) or column index
$msg = $msg.'. '.$failure->errors()[0]; // Actual error messages from Laravel validator
// $msg = $msg.' : met waarde '.$failure->values(); // The values of the row that has failed: not available in version
}
return back()->with('error', $msg);
}
}
return back()->with('error', 'Lêer nie gevind nie');
}
我的导入 class:
class FPImport implements ToModel, WithValidation, WithStartRow
{
public function startRow(): int
{
return 2;
}
public function model(array $row)
{
return new FertilProd([
//
'register_no'=> $row[0],
'product_type'=> $row[1], // id of prodtype tb
'formulation_type'=> $row[2], // id of formulation type tb
'description'=> $row[3],
'formulation_metric'=> $row[4], // id of formulation metric tb
'SAP_no'=> $row[5],
'N'=> $row[6],
'P'=> $row[7],
'K'=> $row[8],
'Zn'=> $row[9],
'Ca'=> $row[10],
'Mg'=> $row[11],
'S'=> $row[12],
'B'=> $row[13],
'Cu'=> $row[14],
'Fe'=> $row[15],
'Mn'=> $row[16],
'Mo'=> $row[17],
'Ni'=> $row[18],
'Se'=> $row[19],
'SG'=> $row[20],
]);
}
public function rules(): array
{
return [
'register_no' => 'required|string',
'product_type' => 'required|integer|min:1', //validate unsigned integer
'formulation_type' => 'required|integer|min:1',
'description'=> 'required|string',
'formulation_metric'=> 'required|integer|min:1',
'SAP_no'=> 'required|string',
'N' => 'required|numeric|between:0,99.9999',
'P' => 'required|numeric|between:0,99.9999',
'K' => 'required|numeric|between:0,99.9999',
'Zn' => 'required|numeric|between:0,99.9999',
'Ca' => 'required|numeric|between:0,99.9999',
'Mg' => 'required|numeric|between:0,99.9999',
'S' => 'required|numeric|between:0,99.9999',
'B' => 'required|numeric|between:0,99.9999',
'Cu' => 'required|numeric|between:0,99.9999',
'Fe' => 'required|numeric|between:0,99.9999',
'Mn' => 'required|numeric|between:0,99.9999',
'Mo' => 'required|numeric|between:0,99.9999',
'Ni' => 'required|numeric|between:0,99.9999',
'Se' => 'required|numeric|between:0,99.9999',
'SG' => 'required|numeric|between:0,99.9999',
];
}
public function customValidationMessages()
{
return [
'register_no' => 'Registrasie kolom moet letter waarde wees',
'product_type' => 'Tipe produk kolom moet volgetal wees',
'product_type' => 'Formulasie tipe kolom moet volgetal wees',
'description'=> 'Beskrywing kolom moet letter waarde wees',
'formulation_metric'=> 'Formulasie mate kolom moet volgetal wees',
'SAP_no'=> 'SAP no kolom moet letter waarde wees',
'N' => 'N kolom moet waarde wees tussen 0,99.9999',
'P' => 'P kolom moet waarde wees tussen 0,99.9999',
'K' => 'K kolom moet waarde wees tussen 0,99.9999',
'Zn' => 'Zn kolom moet waarde wees tussen 0,99.9999',
'Ca' => 'Ca kolom moet waarde wees tussen 0,99.9999',
'Mg' => 'Mg kolom moet waarde wees tussen 0,99.9999',
'S' => 'S kolom moet waarde wees tussen 0,99.9999',
'B' => 'B kolom moet waarde wees tussen 0,99.9999',
'Cu' => 'Cu kolom moet waarde wees tussen 0,99.9999',
'Fe' => 'Fe kolom moet waarde wees tussen 0,99.9999',
'Mn' => 'Mn kolom moet waarde wees tussen 0,99.9999',
'Mo' => 'Mo kolom moet waarde wees tussen 0,99.9999',
'Ni' => 'Ni kolom moet waarde wees tussen 0,99.9999',
'Se' => 'Se kolom moet waarde wees tussen 0,99.9999',
'SG' => 'SG kolom moet waarde wees tussen 0,99.9999',
];
}
}
更新
所以我通过使用 ToCollection
关注点来导入它。但它现在不验证行。当我通过将字符串放置在必须是整数的位置来测试它时,它仍然尝试导入。我收到来自 SQL 错误处理程序的错误。
另一个更新: 所以我回到使用 ToModel
问题,因为 Collection
没有验证 csv。我还注意到我没有在我的导入文件中声明 use Illuminate\Validation\Rule;
包。声明后没有解决。我将 rules()
函数中的变量引用更改为行的索引,然后逐行验证。但现在它卡在 0 field of row 14 is required
的失败消息中。第 14 行第一列的第一个单元格已填充,因此不应出现 failure->error()[0]
消息。
谁能帮我理解为什么导入规则总是说一个字段是必需的,即使它存在?
我解决了我的答案。我问题的最后更新是正确答案。有一个空值,这意味着它是必需的。尽管空值与 14 在不同的行。错误行的原因是因为 failures
数组对象上的 foreach
循环将仅 return 验证后的最后一行。
首先我应该在我的导入文件中声明包:use Illuminate\Validation\Rule;
。
其次,我应该在我的 rules()
函数中使用行的索引作为我的规则的变量,而不是模型 $fillable
声明的名称,即:
/**
* @return array
*/
public function rules(): array
{
return [ // use indexes as variables
'0' => 'required|string',
'1' => 'required|integer|min:1', //validate unsigned integer
'2' => 'required|integer|min:1',
'3'=> 'required|string',
'4'=> 'required|integer|min:1',
'5'=> 'required|string',
'6' => 'required|numeric|between:0,99.9999',
'7' => 'required|numeric|between:0,99.9999',
'8' => 'required|numeric|between:0,99.9999',
'9' => 'required|numeric|between:0,99.9999',
'10' => 'required|numeric|between:0,99.9999',
'11' => 'required|numeric|between:0,99.9999',
'12' => 'required|numeric|between:0,99.9999',
'13' => 'required|numeric|between:0,99.9999',
'14' => 'required|numeric|between:0,99.9999',
'15' => 'required|numeric|between:0,99.9999',
'16' => 'required|numeric|between:0,99.9999',
'17' => 'required|numeric|between:0,99.9999',
'18' => 'required|numeric|between:0,99.9999',
'19' => 'required|numeric|between:0,99.9999',
'20' => 'required|numeric|between:0,99.9999',
];
}
它现在导入并验证。我无法让自定义验证消息起作用。
要使自定义验证消息正常工作,您必须将 .* 添加到您的密钥中
public function customValidationMessages()
{
return [
'0.*' => 'Custom message for :attribute.',
'4.numeric' => ':attribute should be numeric.',
];
}
<?php
namespace App\Imports;
use App\Client;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\Importable;
class ClientsImport implements ToModel, WithStartRow, WithValidation
{
use Importable;
public function startRow(): int
{
return 2;
}
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Client([
//
]);
}
public function rules(): array
{
return [
'0' => 'required',
'4' => 'numeric',
];
}
public function customValidationMessages()
{
return [
'0.*' => 'Custom message for :attribute.',
'4.numeric' => ':attribute should be numeric.',
];
}
public function customValidationAttributes()
{
return [
'0' => 'first name',
'4' => 'Home Phone',
];
}
}
这意味着您的自定义错误消息将适用于所有必需的信息,无论是最小值、最大值还是数字。如果您想要任何自定义消息让我们只说数字,您可以添加 .numeric
enter image description here
我正在使用 Laravel maatwebsite 3.1.3 行验证来验证 csv 文件导入。但是我的 Imports php 文件中的 WithValidation
问题不断抛出一个错误,即第二个导入行的最后一个值是必需的,即使该值存在也是如此。
当我删除验证时,导入工作正常。
我的控制器调用导入函数:
public function doImport(Request $request) {
$this->validate($request, [
'import_file' => 'required|mimes:xls,csv,xlsx,txt' // txt is needed for csv mime type validation
]);
if($request->file('import_file')) {
try {
Excel::import(new FPImport, $request->file('import_file'));
return redirect('admin/fertil/products')->with('success', 'Invoer Sukses');
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
$msg = '';
$failures = $e->failures();
foreach ($failures as $failure) {
$msg = 'Ry '.$failure->row(); // row that went wrong
$msg = $msg.' vir hoof '.$failure->attribute(); // either heading key (if using heading row concern) or column index
$msg = $msg.'. '.$failure->errors()[0]; // Actual error messages from Laravel validator
// $msg = $msg.' : met waarde '.$failure->values(); // The values of the row that has failed: not available in version
}
return back()->with('error', $msg);
}
}
return back()->with('error', 'Lêer nie gevind nie');
}
我的导入 class:
class FPImport implements ToModel, WithValidation, WithStartRow
{
public function startRow(): int
{
return 2;
}
public function model(array $row)
{
return new FertilProd([
//
'register_no'=> $row[0],
'product_type'=> $row[1], // id of prodtype tb
'formulation_type'=> $row[2], // id of formulation type tb
'description'=> $row[3],
'formulation_metric'=> $row[4], // id of formulation metric tb
'SAP_no'=> $row[5],
'N'=> $row[6],
'P'=> $row[7],
'K'=> $row[8],
'Zn'=> $row[9],
'Ca'=> $row[10],
'Mg'=> $row[11],
'S'=> $row[12],
'B'=> $row[13],
'Cu'=> $row[14],
'Fe'=> $row[15],
'Mn'=> $row[16],
'Mo'=> $row[17],
'Ni'=> $row[18],
'Se'=> $row[19],
'SG'=> $row[20],
]);
}
public function rules(): array
{
return [
'register_no' => 'required|string',
'product_type' => 'required|integer|min:1', //validate unsigned integer
'formulation_type' => 'required|integer|min:1',
'description'=> 'required|string',
'formulation_metric'=> 'required|integer|min:1',
'SAP_no'=> 'required|string',
'N' => 'required|numeric|between:0,99.9999',
'P' => 'required|numeric|between:0,99.9999',
'K' => 'required|numeric|between:0,99.9999',
'Zn' => 'required|numeric|between:0,99.9999',
'Ca' => 'required|numeric|between:0,99.9999',
'Mg' => 'required|numeric|between:0,99.9999',
'S' => 'required|numeric|between:0,99.9999',
'B' => 'required|numeric|between:0,99.9999',
'Cu' => 'required|numeric|between:0,99.9999',
'Fe' => 'required|numeric|between:0,99.9999',
'Mn' => 'required|numeric|between:0,99.9999',
'Mo' => 'required|numeric|between:0,99.9999',
'Ni' => 'required|numeric|between:0,99.9999',
'Se' => 'required|numeric|between:0,99.9999',
'SG' => 'required|numeric|between:0,99.9999',
];
}
public function customValidationMessages()
{
return [
'register_no' => 'Registrasie kolom moet letter waarde wees',
'product_type' => 'Tipe produk kolom moet volgetal wees',
'product_type' => 'Formulasie tipe kolom moet volgetal wees',
'description'=> 'Beskrywing kolom moet letter waarde wees',
'formulation_metric'=> 'Formulasie mate kolom moet volgetal wees',
'SAP_no'=> 'SAP no kolom moet letter waarde wees',
'N' => 'N kolom moet waarde wees tussen 0,99.9999',
'P' => 'P kolom moet waarde wees tussen 0,99.9999',
'K' => 'K kolom moet waarde wees tussen 0,99.9999',
'Zn' => 'Zn kolom moet waarde wees tussen 0,99.9999',
'Ca' => 'Ca kolom moet waarde wees tussen 0,99.9999',
'Mg' => 'Mg kolom moet waarde wees tussen 0,99.9999',
'S' => 'S kolom moet waarde wees tussen 0,99.9999',
'B' => 'B kolom moet waarde wees tussen 0,99.9999',
'Cu' => 'Cu kolom moet waarde wees tussen 0,99.9999',
'Fe' => 'Fe kolom moet waarde wees tussen 0,99.9999',
'Mn' => 'Mn kolom moet waarde wees tussen 0,99.9999',
'Mo' => 'Mo kolom moet waarde wees tussen 0,99.9999',
'Ni' => 'Ni kolom moet waarde wees tussen 0,99.9999',
'Se' => 'Se kolom moet waarde wees tussen 0,99.9999',
'SG' => 'SG kolom moet waarde wees tussen 0,99.9999',
];
}
}
更新
所以我通过使用 ToCollection
关注点来导入它。但它现在不验证行。当我通过将字符串放置在必须是整数的位置来测试它时,它仍然尝试导入。我收到来自 SQL 错误处理程序的错误。
另一个更新: 所以我回到使用 ToModel
问题,因为 Collection
没有验证 csv。我还注意到我没有在我的导入文件中声明 use Illuminate\Validation\Rule;
包。声明后没有解决。我将 rules()
函数中的变量引用更改为行的索引,然后逐行验证。但现在它卡在 0 field of row 14 is required
的失败消息中。第 14 行第一列的第一个单元格已填充,因此不应出现 failure->error()[0]
消息。
谁能帮我理解为什么导入规则总是说一个字段是必需的,即使它存在?
我解决了我的答案。我问题的最后更新是正确答案。有一个空值,这意味着它是必需的。尽管空值与 14 在不同的行。错误行的原因是因为 failures
数组对象上的 foreach
循环将仅 return 验证后的最后一行。
首先我应该在我的导入文件中声明包:use Illuminate\Validation\Rule;
。
其次,我应该在我的 rules()
函数中使用行的索引作为我的规则的变量,而不是模型 $fillable
声明的名称,即:
/**
* @return array
*/
public function rules(): array
{
return [ // use indexes as variables
'0' => 'required|string',
'1' => 'required|integer|min:1', //validate unsigned integer
'2' => 'required|integer|min:1',
'3'=> 'required|string',
'4'=> 'required|integer|min:1',
'5'=> 'required|string',
'6' => 'required|numeric|between:0,99.9999',
'7' => 'required|numeric|between:0,99.9999',
'8' => 'required|numeric|between:0,99.9999',
'9' => 'required|numeric|between:0,99.9999',
'10' => 'required|numeric|between:0,99.9999',
'11' => 'required|numeric|between:0,99.9999',
'12' => 'required|numeric|between:0,99.9999',
'13' => 'required|numeric|between:0,99.9999',
'14' => 'required|numeric|between:0,99.9999',
'15' => 'required|numeric|between:0,99.9999',
'16' => 'required|numeric|between:0,99.9999',
'17' => 'required|numeric|between:0,99.9999',
'18' => 'required|numeric|between:0,99.9999',
'19' => 'required|numeric|between:0,99.9999',
'20' => 'required|numeric|between:0,99.9999',
];
}
它现在导入并验证。我无法让自定义验证消息起作用。
要使自定义验证消息正常工作,您必须将 .* 添加到您的密钥中
public function customValidationMessages()
{
return [
'0.*' => 'Custom message for :attribute.',
'4.numeric' => ':attribute should be numeric.',
];
}
<?php
namespace App\Imports;
use App\Client;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\Importable;
class ClientsImport implements ToModel, WithStartRow, WithValidation
{
use Importable;
public function startRow(): int
{
return 2;
}
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Client([
//
]);
}
public function rules(): array
{
return [
'0' => 'required',
'4' => 'numeric',
];
}
public function customValidationMessages()
{
return [
'0.*' => 'Custom message for :attribute.',
'4.numeric' => ':attribute should be numeric.',
];
}
public function customValidationAttributes()
{
return [
'0' => 'first name',
'4' => 'Home Phone',
];
}
}
这意味着您的自定义错误消息将适用于所有必需的信息,无论是最小值、最大值还是数字。如果您想要任何自定义消息让我们只说数字,您可以添加 .numeric enter image description here