我使用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 (MaatwebsiteExcelValidatorsValidationException $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 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 IlluminateValidationRule;
包。这并没有在声明后解决问题。我将rules()
函数中的变量引用更改为行的索引,然后逐行验证。但现在它被一条失败消息卡住了,那就是0 field of row 14 is required
。第14行第一列中的第一个单元格已填充,因此不应出现failure->error()[0]
消息。
有人能帮我理解为什么导入规则总是说字段是必需的,即使它存在?
我解决了我的答案。我问题的最后一次更新是正确的答案。有一个空值,这意味着它是必需的。尽管空值在另一行上为14。错误行的原因是failures
数组对象上的foreach
循环将只返回验证后的最后一行。
首先,我应该在导入文件中声明包:use IlluminateValidationRule;
。
其次,我应该在我的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 AppImports;
use AppClient;
use MaatwebsiteExcelConcernsToModel;
use MaatwebsiteExcelConcernsWithStartRow;
use MaatwebsiteExcelConcernsWithValidation;
use MaatwebsiteExcelConcernsImportable;
class ClientsImport implements ToModel, WithStartRow, WithValidation
{
use Importable;
public function startRow(): int
{
return 2;
}
/**
* @param array $row
*
* @return IlluminateDatabaseEloquentModel|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在此处输入图像描述