我试图验证日期,但是它不起作用,我试图从零件中重构日期,并且在我的sanitize函数中,我添加了要在请求中验证的值。我传递的日期是尝试引发错误的无效日期,但是我无法在请求类中使用它。我输入的日期是2020-02-30,因为该日期不存在,该日期无效
$this->request->add(['event_date' => date('Y-m-d', strtotime($this->request->get('month') . '/' . $this->request->get('day') . '/' . $this->request->get('year')))]);
$input = $this->all();
$input['owner_firstname'] = filter_var($input['owner_firstname'], FILTER_SANITIZE_STRING);
....
....
$this->merge($input);
$rules = [
'owner_firstname' => 'required|min:2|max:30',
'owner_lastname' => 'required|min:2|max:30',
'owner_email' => 'email',
'owner_type' => 'required',
'partner_firstname' => 'required|min:2|max:30',
'partner_lastname' => 'required|min:2|max:30',
'partner_email ' => 'email',
'partner_type' => 'required',
'month' => 'not_in:0',
'day' => 'not_in:0',
'year' => 'required|digits:4|integer|min:' . date('Y') . '|max:'. (date('Y', strtotime('+9 months'))),
'event_date' => 'date',
'event_city' => 'required|not_in:0',
'event_country' => 'required|not_in:0',
'number_of_guests' => 'required',
'guests_message' => 'required|min:30|max:5000',
];
答案 0 :(得分:0)
2件事:
1)由于您构造日期字符串以发送给验证者的方式,验证已通过。
date('Y-m-d', strtotime('02/30/2020')) // This becomes "2020-03-01"
因此,由于2020年3月1日为有效日期,因此验证通过。相反,您可以直接用破折号构造日期字符串。
2)在某些情况下(例如AJAX请求),在调用$this->request->add()
时不会将参数添加到调用$this->all()
时得到的结果中。尝试使用merge()
添加参数。
// This will set the date as '2020-02-30' and fail validation
$this->merge(['event_date' => $this->request->get('year') . '-' . $this->request->get('month') . '-' . $this->request->get('day')]);