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

时间:2016-11-16 17:04:54

标签: php laravel laravel-5

我有一系列的开放时间和关闭时间。

输入看起来像这样:

 <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"之间存在重叠。验证不应该通过。

2 个答案:

答案 0 :(得分:1)

  

这不是一个完整的答案,但它可能有助于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'并在整个数组中进行验证。

  

据我所知,天是验证时间的相关属性,因此我将其添加为参数,但您可以更改它。请查看dd()输出中OverlappingTimeValidator@validate的内容。

为了添加错误信息,请打开./resources/lang/en/validation.php并添加(靠近底部,无所谓):

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

答案 1 :(得分:-1)

您可以尝试

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

参考此链接

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

相关问题