Search code examples
laravel-5.6laravel-validation

How is this value failing my laravel date validation?


So I am posting to an api controller action, as we can see I have some basic validation:

public function store(Request $request) {
        $fields = $request->except(['_token']);
        $user   = $request->user();

        $request->validate([
            'log_date' => 'required|date_format:"Y-m-d"',
            'bill_time' => 'required|numeric',
        ]);
}

I constantly get: The log date does not match the format Y-m-d.

The date coming in is formatted by moment js and shows:

"2018-11-6"

So I am confused how is that not formatted properly?


Solution

  • When using Y-m-d, the d on that format mask applies the following:

    Day of the month, 2 digits with leading zeros

    So, the value being passed 2018-11-6 fails due to 6 not matching 06. Either use Y-m-j, where j is

    Day of the month without leading zeros

    Or adjust how moment is sending your value.

    For full date reference, check http://php.net/manual/en/function.date.php