验证日期不在日期范围的数组之间

时间:2019-06-15 19:35:45

标签: laravel

我正在尝试验证这一点:

“如果某个日期已经过,一个人就不能在该日期范围内休假”

所以,如果我在表中有这样的东西假期

start_date  |  end_date  | duration_days | user_id
--------------------------------------------------
 01-01-2019 | 01-02-2019 |      30       |   1
 03-02-2019 | 28-02-2019 |      15       |   2
 12-12-2019 | 01-01-2020 |      20       |   3

如您所见,所有日期都是互斥的

如果我想再添加一行,我必须对此进行验证(我认为是这样,如果我错了,请纠正我):

  • start_date 不在中:

    [{{01-01-2019; 01-02-2019},{03-02-2019; 28-02-2019},{03-02-2019; 28-02-2019}]。

  • end_date 不在中:

    [{{01-01-2019; 01-02-2019},{03-02-2019; 28-02-2019},{03-02-2019; 28-02-2019}]。

将所有这些逻辑应用于 VacationController 存储区中的 laravel

public function store(Request $request, $user_id)
{
        $dates_taken = Vacation::all('start_date', 'end_date');

        $request->validate([
            'start_date' => 'required|date|not_in:'.$dates_taken,
            'end_date' => 'required|date|not_in:'.$dates_taken',
        ]);
}

我知道验证是错误的,但是我想了解这个问题。

可以使用laravel validation来完成此操作吗?

3 个答案:

答案 0 :(得分:3)

您应该使用自定义验证,并可以使用Validator::extend()来定义自定义规则,例如:

Validator::extend('exclude_pre_leaves', function ($attribute, $value, $p, $validator) {
    $data = $validator->getData();
    $hasMached = Vacation::where('start_date', '>=', $data[$p[0]])
                            ->where('end_date','<=', $data[$p[0]])
                            ->orWhere(function($q) use($p, $data){
                                        $q->where('start_date', '>=', $data[$p[1]])
                                          ->where('end_date','<=', $data[$p[1]]);
                            })->count();

    return $hasMatched == 0;
});

您可以这样使用:

$request->validate([
    'start_date' => 'required|date|exclude_pre_leaves:start_date,end_date',
    'end_date' => 'required|date|exclude_pre_leaves:start_date,end_date'
]);

答案 1 :(得分:1)

可以选择创建我们自己的规则

  

php artisan make:rule VacationDateRule

这将创建名称为新文件

VacationDateRule.phpApp\Rules目录内

之后,我将您的模型视为假期

复制并粘贴规则文件

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use App\Vacation;

class VacationDateRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct($form ,$to ,$userid)
    {
        $this->form = $form; 
        $this->to = $to;
        $this->userid = $userid;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $vacation = Vacation::where('start_date','>=',$this->form)
                            ->where('end_date','<=',$this->to)->count();
        $vacation === 0 ? true : false ;
        //
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The validation error message.';
    }
}

并在验证中

 $request->validate([
        'start_date' => ['required|date',new VacationDateRule(
            $request->start_date,
            $request->start_date,
            $request->user_id)],
        'end_date' => 'required|date',
    ]);

如果您有任何问题,请在下面评论

答案 2 :(得分:0)

根据需要使用自己的验证。在验证中实施您自己的算法

 $validationarray = $this->validate($request,
            [
                'start_date'  => [
                    'required',
                    'date',
                    function ($attribute, $value, $fail) { 
                        //$attribute->input name, $value for that value.
                        //or your own way to collect data. then check.
                        //make your own condition.
                        if($this->dateBetween($value)) {                        
                             $fail($attribute.' is failed custom rule. The date is not acceptable.');
                        }
                    },          
                ],    
                'end_date'  => [
                    'required',
                    'date',
                    function ($attribute, $value, $fail) { 
                        //$attribute->input name, $value for that value.
                        //or your own way to collect data. then check.
                        //make your own condition.
                        if($this->dateBetween($value)) {                        
                             $fail($attribute.' is failed custom rule. The date is not acceptable.');
                        }
                    },          
                ],   
            ]);

将该函数写入相同的类。原因是我将其用于验证。

public function dateBetween($date){
    if(checkDateAsDesired) return true;
    else return false;   
}