laravel require_if 日期验证

'use_dates'  => 'required|string|max:3',
'start_date' => 'required_if:use_dates,Yes|date_format:d/m/Y',
'end_date' => 'required_if:use_dates,Yes|date_format:d/m/Y|after:start_date',


 $validator = Validator::make($data, [
       
            'start_date' => 'required_if:type,0|date_format:d/m/Y',

        ], [
            'required' => ':attribute必须填写',
        ], [
            
            'start_date' => '开始时间',
        ]);

        $err = [
            'start_date' => '开始时间',
          
        ];

        if ($validator->fails()) {
            $valid_string = '';
            foreach ($validator->errors()->messages() as  $k => $message) {
                if(isset($err[$k])){
                    $valid_string .= '<br/>' . $err[$k]."必须填写";
                }else{
                    $valid_string .= '<br/>' . $message[0];
                }
            }

            return $this->error([], '验证未通过:'.$valid_string);
        }

posted @ 2023-03-03 12:22  Bashuslovakia  阅读(22)  评论(0编辑  收藏  举报