Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kennethjaysone's avatar

Laravel 5 validate date as php Y format e.g 2015?

I have a form that uses bootstraps datepicker. I want to collect the year as 2015 for example

How do i do that?

The datepicker code is this:

$('#manufacture_year').datepicker({
    format: " yyyy",
    viewMode: "years",
    minViewMode: "years",
    endDate: FromendDate,
    autoclose: true,
    disableEntry: true
});

I'm displaying the datepicker like so:

{!! Form::text('manufacture_year', null, ['placeholder' => '1984', 'class' => 'form-control datepicker-default', 'id' => 'manufacture_year']) !!}

My validation rules are:

'manufacture_year' => 'required|date|date_format:Y'

When i submit the form i keep getting:

The manufacture year is not a valid date.,The manufacture year does not match the format Y.

I put 'Y' as the date format refering to:

PHP Manual

Where it states that 'Y' equates a full numeric representation of a year, 4 digits

Thank you for reading this.

0 likes
5 replies
mstnorris's avatar
Level 55

Remove the date validation rule

'manufacture_year' => 'required|date_format:Y'

Also try putting it in quotes " ".

'manufacture_year' => 'required|date_format:"Y"'
kennethjaysone's avatar

Hi @mstnorris

I've tried

'manufacture_year' => 'required|date_format:"Y"',

and

'manufacture_year' => 'required|date_format:Y',

Still doesn't work mate.

kennethjaysone's avatar

The stupid mistake i made was in the javascript for the datepicker

If you notice my code, there's a space in the format property

$('#manufacture_year').datepicker({
    format: " yyyy", // Notice the space 
    viewMode: "years",
    minViewMode: "years",
    endDate: FromendDate,
    autoclose: true,
    disableEntry: true
});

It should be

$('#manufacture_year').datepicker({
    format: "yyyy",
    viewMode: "years",
    minViewMode: "years",
    endDate: FromendDate,
    autoclose: true,
    disableEntry: true
});
alexmansour's avatar

Hello @mstnorris I'm facing issue with the date_format validation rule where it's accepting 0020 as Y. The current year as: "date_format:d-m-Y" and it's accepting such date: "21-12-0020"

Any thoughts how we can restrict this ?

Please or to participate in this conversation.