Search code examples
laravellaravel-validationmaatwebsite-excel

Validation fails even if it has values Maatwebsite Laravel Validation


I'm currently using Maatwebsite collection when processing the import CSV file and validating it as well since I'm having hard time using the ToModel way. Here's how I validate the csv fields:

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;
    }
}

This is a sample data I have.

Illuminate\Support\Collection {#565 ▼
    #items: array:6 [▼
        0 => "Room name"
        1 => "Room101"
        2 => "user"
        3 => "fmacejkovic@example.org"
        4 => "password"
        5 => "remarks"
    ]
}

My problem now is that even though the values are all correct and valid, it still fails in the validation. I'm trying to assign to a specific variable so that when it fails, it'll return the row name instead of row number. Even though I use the row number, it still fails.


Solution

  • You have used incorrect syntax for Validator::make(), use this :

    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;
            }
        }