在Laravel中多个打开和关闭时间数组验证


Multiple Open and Close Time array validation in Laravel?

我有一个打开时间和关闭时间的数组。

输入如下所示:

 <input type="text" name="time[1][open]">
 <input type="text" name="time[1][close]">
 <input type="text" name="time[x][open]">
 <input type="text" name="time[x][close]">

这是dd($request->all())

的转储
array:2 [
  "days" => array:2 [
    0 => "1" // Mon
    1 => "2" // Tue
  ]
  "time" => array:2 [
    1 => array:2 [
      "open" => "09:00"
      "close" => "11:30"
    ]
    2 => array:2 [
      "open" => "16:00"
      "close" => "21:00"
    ]
  ]
]

使用Laravel请求验证-如何通过打开和关闭时间之间的循环来验证,以确保它不重叠在mysql数据库中的现有记录?例如,我们可能在数据库中有Open:14:00 - Close:19:00,但用户有"open" => "16:00""close" => "21:00"的请求-因此验证不应该通过。

更新:

示例结果在times表中,一天可以有多个打开/关闭时间。

id   |   day    |   open_time  |   close_time
-----------------------------------------------
1    |    1     |   13:30:00   |    15:00:00
2    |    2     |   16:30:00   |    20:00:00
3    |    3     |   09:30:00   |    14:30:00
4    |    3     |   18:00:00   |    22:00:00
-----------------------------------------------

你可以看到有重叠从times.id=2(16:30:00 - 20:00:00)和用户请求"open" => "16:00""close" => "21:00"。验证不应该通过。

这不是一个完整的答案,但它可能会帮助op。我仍然不理解天/时间等之间的关系,这是没有逻辑的模板,需要实现


首先创建新的提供者(用于自定义验证规则)

$ php artisan make:provider ValidatorServiceProvider

并注册到config/app.php

    ...
    App'Providers'RouteServiceProvider::class,
    App'Providers'ValidatorServiceProvider::class,
    ...

为新的验证规则创建新的文件夹/文件:

$ mkdir ./app/Validators
$ touch ./app/Validators/OverlappingTimeValidator.php

并将一些代码放入新的Validator规则中(由于我没有完全理解问题,可能我缺乏语言技能来理解它,因此无法理解逻辑)

<?php
namespace App'Validators;
class OverlappingTimeValidator
{
    public function validate($attribute, $value, $parameters, $validator)
    {
        //make sure you have valid time format (for each time[])
        //make sure open < close for each pair
        //compare each set of open-close times with each other
        //connect to database and get all required data from $parameters[0] table
        //to connect to database and get data use 'DB::table($parameters[0])->get(); follow this https://laravel.com/docs/5.3/queries#retrieving-results
        //array_get($validator->getData(), $parameters[1], null); // gives you days user picked or null
        dd($attribute, $value, $parameters, $validator);
        return false; //in case validation fails
        return true; //in case validation passes
    }
}

运行$ composer dump-autoload使新文件自动加载

在ValidatorServiceProvider.php中注册验证规则

public function boot()
{
    Validator::extend('overlapping_time', 'App'Validators'OverlappingTimeValidator@validate');
}

最后在*Request中的规则应该是这样的:

public function rules()
{
    return [
        'time' => 'overlapping_time:table_name,days'
    ];
}

您可以使用'time.*',验证将分别针对每次运行,这可能不是您想要的!因此,使用'time'并对整个数组进行验证。

因为我理解days是验证时间的相关属性,所以我添加了它作为参数,但是您可以更改它。请查看OverlappingTimeValidator@validatedd()的输出

为了添加错误信息,打开./resources/lang/en/validation.php并添加(靠近底部,真的无关紧要):

'overlapping_time' => 'The :attribute...',

你可以试试

 'time.*.open' => 'required',
 'time.*.close' => 'after:time.*.open',

参考此链接

https://laravel.com/docs/5.3/validation validating-arrays