laravel 如何验证跨越午夜的两个值之间的时间(15:00 > 时间 > 01:00)

laravel how to validate time between two values that crosses midnight (15:00 > time > 01:00)

我有一个如下所示的请求

"date" => "2022-05-25"
"shift" => "2"
"attendance" => array:1 [▼
  4 => array:5 [▼
    "present" => "on"
    "employee_id" => "4"
    "time_in" => "15:00"
    "time_out" => "01:00"
    "note" => null
  ]

知道每个班次都有自己的开始和离开时间,每个班次也可以选择跨越午夜。对于这种情况,我们有这样的转变

#attributes: array:6 [▼
    "id" => 2
    "title" => "Evening Shift"
    "starting_time" => "15:00"
    "leaving_time" => "01:00"
    "across_midnight" => 1
    "user_id" => 1
  ]

我需要的是验证 time_intime_out 是在 starting_timeleaving_time

之间

例如,这里(在这种情况下)有效值可以是 17:00、18:00、22:00、00:30 而 14::00 不是有效值

这是我现在的验证规则,如果班次不超过午夜,它就可以正常工作

public function rules()
    {
        $shift = Shift::find($this->shift);
        $rules = [
            'date' => 'required|date|date_format:Y-m-d',
            'shift' => 'required|exists:shifts,id,user_id,' . auth()->id(),
            'attendance' => 'required|array',
            'attendance.*.employee_id' => 'required|exists:employees,id,user_id,' . auth()->id(),
        ];

        foreach ($this->attendance as $key => $Value) {
            $rules['attendance.' . $key . '.time_in'] = [Rule::requiredIf($this->has('attendance.' . $key . '.present')), 'date_format:H:i', 'nullable', 'after_or_equal:' . $shift?->starting_time, 'before_or_equal:' . $shift?->leaving_time];
            $rules['attendance.' . $key . '.time_out'] = [Rule::requiredIf($this->has('attendance.' . $key . '.present')), 'date_format:H:i', 'nullable', 'after_or_equal:' . $shift?->starting_time, 'before_or_equal:' . $shift?->leaving_time];
        }

        return $rules;
    }

这是我在午夜轮班时得到的结果

The Time In must be a date before or equal to 01:00.

The Time Out must be a date after or equal to 15:00.

您可以编写自己的验证规则,我推荐使用rule object validation。 但是如果你想使用 validation in clousures 你可以这样做:


$rules = [
    'time_in' => function ($attribute, $value, $fail) {
            if ((new \Carbon\Carbon($value))->lessThan(new \Carbon\Carbon($shift?->starting_time))) {
                $fail('The '.$attribute.' has to be less Than starting time');
            }
        }         
];

验证规则很简单,写下你需要的复杂度。

我终于找到了解决办法。

我使用命令 'php artisan make:rule CrossMidnightTimeValidation'

创建了一个规则

在创建的文件中

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class CrossMidnightTimeValidation implements Rule
{
    private $starting_time,$end_time;

    public function __construct($starting_time, $end_time)
    {
        $this->starting_time = $starting_time;
        $this->end_time = $end_time;

    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute,  $value)
    {
        return ($value >= $this->starting_time && $value <= now()->endOfDay()->format('H:i'))
            || (($value >= now()->startOfDay()->format('H:i') && $value <= $this->end_time));
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'Invalid Time (out of the shift time).';
    }
}

我的规则。

public function rules()
    {
        $shift = Shift::find($this->shift);
        $rules = [
            'date' => 'required|date|date_format:Y-m-d',
            'shift' => 'required|exists:shifts,id,user_id,' . auth()->id(),
            'attendance' => 'required|array',
            'attendance.*.employee_id' => 'required|exists:employees,id,user_id,' . auth()->id(),
        ];
        if ($shift->across_midnight) {
            foreach ($this->attendance as $key => $value) {
                $rules['attendance.' . $key . '.time_in'] = [Rule::requiredIf($this->has('attendance.' . $key . '.present')), 'date_format:H:i', 'nullable', new CrossMidnightTimeValidation($shift?->starting_time, $shift?->leaving_time)];
                $rules['attendance.' . $key . '.time_out'] = [Rule::requiredIf($this->has('attendance.' . $key . '.present')), 'date_format:H:i', 'nullable', new CrossMidnightTimeValidation($shift?->starting_time, $shift?->leaving_time)];
            }
        }
        else {
            foreach ($this->attendance as $key => $value) {
                $rules['attendance.' . $key . '.time_in'] = [Rule::requiredIf($this->has('attendance.' . $key . '.present')), 'date_format:H:i', 'nullable', 'before_or_equal:attendance.' . $key . '.time_out', 'after_or_equal:' . $shift?->starting_time];
                $rules['attendance.' . $key . '.time_out'] = [Rule::requiredIf($this->has('attendance.' . $key . '.present')), 'date_format:H:i', 'nullable', 'before_or_equal:' . $shift?->leaving_time, 'after_or_equal:' . $shift?->starting_time];
            }
        }
        return $rules;
    }