Ad

Laravel Date Validation Fails With JQuery Datepicker

- 1 answer

In my blade file I have defined:-

<div class="createMesh_row date" id="event_date_div">
    <label>Event Date</label>
    {{ Form::text('event_date','',array('id'=>'event_date','readonly', 'class' => '')) }}
    <i class="fa fa-calendar eventdate"></i>
    {{ $errors->first('event_date','<span class="error">:message</span>') }}
</div>

In my js file:-

 $( "#event_date,#target_date" ).datepicker({
     changeMonth: true,
     changeYear: true,
     dateFormat: 'mm/dd/yyyy',
     minDate: 0        
  });

I have defined rule for this:-

 $rules=[
            'title'       =>'required',
            'type'        =>'required',
            'privacy'     =>'required',
            'target_date' =>'required|date|date_format:"mm-dd-yyyy"',

  //  tried 'target_date' =>'required|date|date_format:"mm/dd/yyyy"',
  //  tried 'target_date' =>'required|date|date_format:"d/m/Y"',
  //  tried'target_date' =>'required|date_format:d-m-Y', 
  //  not working

            'occasion'    =>'required'
        ];

Errors:-

[target_date] => Array
                (
                    [0] => The target date is not a valid date.
                    [1] => The target date does not match the format mm-dd-yyyy.
                )
Ad

Answer

I Wrote

'target_date' =>'required|date_format:"m/d/Y"',

and in Js, Do not define date format.

$("#target_date,#event_date").datepicker({
        changeMonth: true,
        changeYear: true,
        //  dateFormat: 'mm/dd/yy', // Hide this line
        minDate: 0
});

Working fine for me :)

Ad
source: stackoverflow.com
Ad