【发布时间】:2020-05-16 18:17:30
【问题描述】:
我目前在处理导入 CSV 文件并对其进行验证时正在使用 Maatwebsite 集合,因为我很难使用 ToModel 方式。以下是我验证 csv 字段的方法:
class ImportRooms implements ToCollection, WithStartRow
{
public function collection(Collection $rows)
{
foreach($rows as $row){
\Validator::make($row->toArray(), [
'name' => $row[0],
'room_code' => $row[1],
'user_name' => $row[2],
'email' => $row[3],
'password' => $row[4],
'remarks' => $row[5],
'name' => ['required', 'max:50'],
'room_code' => ['required', 'max:50'],
'user_name' => ['required', 'max:255'],
'email' => ['required', 'email', 'max:255','nullable'],
'password' => ['min:8','max:255','nullable'],
'remarks' => ['max:500'],
])->validate();
}
}
/**
* @return int
*/
public function startRow(): int
{
return 2;
}
}
这是我的示例数据。
Illuminate\Support\Collection {#565 ▼
#items: array:6 [▼
0 => "Room name"
1 => "Room101"
2 => "user"
3 => "fmacejkovic@example.org"
4 => "password"
5 => "remarks"
]
}
我现在的问题是,即使这些值都是正确且有效的,它仍然无法验证。我试图分配给一个特定的变量,这样当它失败时,它将返回行名而不是行号。即使我使用了行号,它仍然失败。
【问题讨论】:
标签: laravel laravel-validation maatwebsite-excel