【问题标题】:How to validate time in laravel如何在laravel中验证时间
【发布时间】:2017-01-20 21:40:13
【问题描述】:

我想在 Laravel 中验证时间。例如:-我希望当用户输入晚上 8 点到晚上 10 点之间的时间时,它会显示验证错误。我怎样才能在 Laravel 中实现这一点

【问题讨论】:

标签: php laravel laravel-validation


【解决方案1】:

创建 DateRequest 然后添加

<?php

namespace App\Http\Requests\Date;

use App\Http\Requests\FormRequest;


class DateRequest extends FormRequest
{
    /**
     * --------------------------------------------------
     * Determine if the user is authorized to make this request.
     * --------------------------------------------------
     * @return bool
     * --------------------------------------------------
     */
    public function authorize(): bool
    {
        return true;
    }


    /**
     * --------------------------------------------------
     * Get the validation rules that apply to the request.
     * --------------------------------------------------
     * @return array
     * --------------------------------------------------
     */
    public function rules(): array
    {
        return [

            'start_date' => 'nullable|date|date_format:H:i A',
            'end_date' => 'nullable|date|after_or_equal:start_date|date_format:H:i A'
        ];
    }
}

【讨论】:

  • 就像评论一样,根据 laravel 文档“验证字段时应该使用 date 或 date_format,而不是两者”
【解决方案2】:
$beginHour = Carbon::parse($request['hour_begin']);
        $endHour = Carbon::parse($request['hour_end']);
        if($beginHour->addMinute()->gt($endHour)){
            return response()->json([
                'message' => 'end hour should be after than begin hour',
            ], 400);
        }

【讨论】:

    【解决方案3】:

    在 Lavavel 5.6 中:

    在位于 /app/Http/Requests/YourRequestValidation 的文件中

    namespace App\Http\Requests;
    
    use Illuminate\Foundation\Http\FormRequest;
    
    class YourRequestValidation extends FormRequest
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return true;
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                'initial_time' => 'required',
                'end_time' => 'required',
                'end_time' => 'after:initial_time'
            ];
        }
    
        /**
         * Custom message for validation
         *
         * @return array
         */
        public function messages()
        {
    
            return [
                'initial_time.required' => 'Please, fill in the initial time',
                'end_time.required' => 'Please, fill in the end time.',
                'end_time.after' => 'The end time must be greater than initial time.'
            ];
    
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      使用 date_format 规则验证

      date_format:H:i
      

      来自文档

      date_format:format
      

      验证中的字段必须匹配根据 date_parse_from_format PHP 函数定义的格式。

      【讨论】:

        【解决方案5】:

        这段代码可能会在您的控制器中运行。但是,它不会验证不同日期的时间(例如晚上 9 点 - 第二天凌晨 3 点)。 time_starttime_end 在这种情况下应提供为 HH:mm 但您可以轻松更改它。

        public function store(Illuminate\Http\Request $request)
        {
            $this->validate($request, [
                'time_start' => 'date_format:H:i',
                'time_end' => 'date_format:H:i|after:time_start',
            ]);
        
            // do other stuff
        }
        

        【讨论】:

        • 不适用于 Laravel 5.6。在一个表格上包含秒数。 'time_start' =&gt; 'date_format:H:i:s', 'time_end' =&gt; 'date_format:H:i|after:time_start',
        【解决方案6】:

        试试这个代码

        use Validator;
        use Carbon\Carbon;
        
        
        $timeHours = "7:00 PM";//change it to 8:00 PM,9:00 PM,10:00 PM  it works
        $time = Carbon::parse($timeHours)->format('H');
        
        
        $request['time'] = $time;
        $validator = Validator::make($request->all(), [
            'time' => ['required','integer','between:20,22']
        ]);
        
        
         if ($validator->fails()) {
            dd($validator->errors());
        }
        

        【讨论】:

          【解决方案7】:

          您可能应该查看this 位的文档。自定义规则听起来像是您要走的路。

          【讨论】:

          • 我知道,但我想知道这次如何验证的逻辑
          • 看看这个:carbon.nesbot.com 然后你就可以轻松地处理时间了。例如:$dt = Carbon::parse('2012-9-5 23:26:11.123789'); var_dump($dt->小时);
          猜你喜欢
          • 2020-04-30
          • 2020-07-10
          • 2021-06-01
          • 2022-11-24
          • 1970-01-01
          • 2015-07-29
          • 2018-01-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多