无需日期即可更新会议详细信息

时间:2018-07-27 20:48:42

标签: php laravel

我有update()来补充会议的详细信息。

如果用户填写会议名称表格,但未填写start_date和end_date字段,则Carbon.php文件中将出现错误“ InvalidArgumentException数据丢失”。开始日期和结束日期不是必填字段。

 public function update(Request $request, $id)
 {
        $rules = [
            'name' => 'required',
            'start_date' => 'nullable|date_format:"d F Y - H:i"',
            'end_date' => 'nullable|date_format:"d F Y - H:i"|after_or_equal:start_date',
        ];

        $customMessages = [
            'name.required' => 'The field name is required.',
            'start_date.date_format' => 'The field start_date dont have a valid format.',
            'end_date.date_format' => 'The field end_date dont have a valid format.',
            'end_date.after_or_equal' => 'The end_date must be after start_date.'
        ];


        $this->validate($request, $rules, $customMessages);

        $conference = Conference::find($id);

        $conference->name = $request->name;
        $conference->start_date = Carbon::createFromFormat('d F Y - H:i', $request->start_date);
        $conference->end_date = Carbon::createFromFormat('d F Y - H:i', $request->end_date);

        return redirect()->back();
    }

我更改为:

$conference->start_date = ($request->start_date) ? Carbon::createFromFormat('d F Y - H:i', $request->start_date) : '';
        $conference->end_date =   ($request->start_date) ? Carbon::createFromFormat('d F Y - H:i', $request->end_date) : '';

但是也不起作用,因为如果用户不输入任何日期而不是显示成功消息,那么它将显示与该日期相关的其他验证消息:

The field start_date dont have a valid format.
The field end_date dont have a valid format.
The end_date must be after start_date.

0 个答案:

没有答案