验证失败,即使它具有值Maatwebsite Laravel Validation

问题描述 投票:0回答:2

我在处理导入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 => "[email protected]"
        4 => "password"
        5 => "remarks"
    ]
}

我现在的问题是,即使这些值都是正确且有效的,它仍然无法通过验证。我试图分配给一个特定的变量,以便当它失败时,它将返回行名而不是​​行号。即使我使用行号,它仍然会失败。

laravel laravel-validation maatwebsite-excel
2个回答
0
投票

您为Validator::make()使用了不正确的语法,请使用此:

class ImportRooms implements ToCollection, WithStartRow
    {

        public function collection(Collection $rows)
        {
            foreach($rows as $row){
                $row = $row->toArray();
                $data = [
                    'name' => $row[0],
                    'room_code' => $row[1],
                    'user_name' => $row[2],
                    'email' => $row[3],
                    'password' => $row[4],
                    'remarks' => $row[5],
                    ];
                \Validator::make($data, [
                    '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;
        }
    }


0
投票

参考https://laravel.com/docs/5.8/validation#automatic-redirection

//Convert row data into array and store it in a variable.
$row = $row->toArray();
//Set data to be validated.
$data = [
    'name'      => $row[0],
    'room_code' => $row[1],
    'user_name' => $row[2],
    'email'     => $row[3],
    'password'  => $row[4],
    'remarks'   => $row[5]
];
//Set conditions for validation.
$conditions = [
    '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 the excel data.
\Validator::make($data, $conditions)->validate();
© www.soinside.com 2019 - 2024. All rights reserved.