Search code examples
laravellaravel-5date-formatlaravel-5.1laravel-validation

Laravel Validation Date does not match the format


I am trying to validate a date in Laravel. I am using datepicker in the frontend where the JavaScript sends the date to the server Laravel in this format dd-MMMM-yy:

$scope.formData.dob = $filter('date')(scope.formData.dob, 'dd-MMMM-yy');

My laravel has a validation like this:

$validator = Validator::make($postData, [
             'dob'       => 'date_format:"dd-MMMM-yy"',
         ]);

However the validator keeps saying that the date does not match the format dd-MMMM-yy:

The dob does not match the format dd-MMMM-yy.

I thought maybe it has to do with the format type so I tried changing it to different formats like d/m/y, dd-mm-yyyy, etc.. in both JavaScript date and Laravel date validation but everything keeps saying the same error that the date doesn't match. The output seems fine though and it is in the format I have mentioned in validation. I dont understand why Laravel validation fails.

Am I missing something here?


Solution

  • You're using the wrong format. You need to use standard PHP formats, which is what Carbon and, in turn, Laravel's validation uses. You can find them in the PHP Docs for date()

    What you actually want is d-F-y, I believe.