简体   繁体   English

Laravel 表单验证限制更新时的图像数量

[英]Laravel form validation limit no of images while updating

I need form validation to limit no of images(5) per item.我需要表单验证来限制每个项目的图像数量(5)。 My validation works when I am adding a new item but does not work while I update.我的验证在我添加新项目时有效,但在我更新时无效。 when I update it can add more than 5 images.当我更新它可以添加超过 5 个图像。 here is my code --这是我的代码——

Controller Controller

 public function store(Request $request){
    $request->validate([
                'carreg' => 'required|max:10',
                'car_image' => 'array|max:5',
            ]);
            $car = new Car;
      

                ..........................
               ............................
           $car->save();}


public function update(Request $request,id){
        $request->validate([
                    'carreg' => 'required|max:10',
                    'car_image' => 'array|max:5',
                ]);
                $car = Car::find($id);
          
                    .....................
                   ............................
               $car->save();}

The problem is in your code.问题出在您的代码中。 When you insert the data, it contains the 5 images which names are saved in Database.当您插入数据时,它包含 5 个名称保存在数据库中的图像。 Now when you are updating, you are again accepting new images.现在,当您更新时,您再次接受新图像。 Thats not a problem cause when you inserted the data, you probably got only 3 images.这不是问题,因为当您插入数据时,您可能只有 3 张图像。 So you can allow users to update the database with another 2 images.因此,您可以允许用户使用另外 2 个图像更新数据库。 But to do so, you've to check the database before storing the images at update.但要这样做,您必须在更新时存储图像之前检查数据库。

So according to your code's 1st part所以根据你的代码的第一部分

public function store(Request $request){
    $request->validate([
                'carreg' => 'required|max:10',
                'car_image' => 'array|max:5',
            ]);
            $car = new Car;
        ....................................
        ......Data is saving to database with 
        ......5/4/3/2/1/0 images depending on your code.......
        ...........................

           $car->save();
}

So in your 2nd part of the code has to be modified like this所以在你的代码的第二部分必须像这样修改

public function update(Request $request,id){
        $request->validate([
                    'carreg' => 'required|max:10',
                    'car_image' => 'array|max:5',
                ]);
                $car = Car::find($id);
          
                    .....................
                   .....Check if the car has 5/4/3/2/1/0 image in the database.
                   ..... If it has less than 5 images  
                   ......allow (5-image already uploaded) amount of images to upload
                   ..... If user has selected more than the desired amount of image,
                   ......you can upload first nth desired images or return error 
                   ......to user to upload valid amount of file ....
               
              $car->save();
 }

If the user already uploaded 5 images, in edit form, do not show file upload field (hide it, or use if condition to propagate in HTML).如果用户已经上传了 5 张图片,在编辑表单中,不显示文件上传字段(隐藏它,或者使用 if 条件在 HTML 中传播)。 You can also use Javascript to allow certain amount of file to select to upload also.您也可以使用 Javascript 允许一定数量的文件到 select 也可以上传。

You should check the number of images available, before validate, ex:您应该在验证之前检查可用图像的数量,例如:

function update(Request $request, $id)
{
    $car = Car::find($id);
    // length car_image
    $length = count($car->car_image);
    $max = 5 - $length;

    $request->validate([
        'carreg' => 'required|max:10',
        'car_image' => 'array|max:' . $max,
    ]);

    $car->save();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM