【问题标题】:Validation fails even if it has values Maatwebsite Laravel Validation验证失败,即使它有值 Maatwebsite Laravel 验证
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 感谢这个。没看到。还有什么方法可以让我得到错误的行号吗??
    【解决方案2】:

    参考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();
    

    【讨论】:

    • 感谢这个。没看到。还有什么方法可以让我得到错误的行号吗??
    • 是@EemJee 在循环中,设置一个迭代变量,错误合并。
    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2021-10-06
    • 2014-08-16
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多