Validation error using datepicker

Published 4 months ago by johnnw

Im using datepicker in a input field. The datepicker use this format "dd/mm/yy". When I submit the form I get a laravel validation error "The date date is not a valid date." Do you know how to fix?

Laravel

$this->validate($request, [
    'date' => 'required|date',
]);
$pdf = Pdf::create([
    'date' => Carbon::parse($request->date)->format('d/m/Y'),
]);

HTML

<div class="form-group col-md-6">
    <label for="date"Date</label>
    <div class="input-group date" data-provide="datepicker">
        <input type='text' class="form-control"
               value="{{ old('date') }}" name="date" placeholder="DD/MM/YYY"/>
        <span class="input-group-addon"><i class="fa fa-calendar text-primary" aria-hidden="true"></i></span>
    </div>
</div>

JS Format:

$( ".input-group.date" ).datepicker({
    format: 'dd/mm/yy'
});

Best Answer (As Selected By johnnw)
skliche

Do a dump of the request data and have a look at the actual contents of request('date'). Does it really contain a string with a valid date in the given format?

Edit: d/m/Y requires a four digit year (http://php.net/manual/en/datetime.createfromformat.php)

absolute

Update your validation rule to reflect the format of the date you want to receive.


$this->validate($request, [
    'date' => 'required|date_format:d/m/Y',
]);

You should use either the date or date_format methods, so be sure to remove the original date method.

johnnw

Thanks, but now appears "The date date does not match the format d/m/Y"

skliche
skliche
4 months ago (149,490 XP)

Do a dump of the request data and have a look at the actual contents of request('date'). Does it really contain a string with a valid date in the given format?

Edit: d/m/Y requires a four digit year (http://php.net/manual/en/datetime.createfromformat.php)

absolute

Can I just check the date you are entering into the input field through the datepicker? Is it in the format 16/02/2018?

johnnw

The request shows in this format:

"date" => "16/02/18"

And in the datepicker input appears:

"16/02/18"

absolute

Change your JS formatting to be the 4 digit year:

$( ".input-group.date" ).datepicker({
    format: 'dd/mm/yyyy'
});

Please sign in or create an account to participate in this conversation.