Ad

Laravel - Inputting 'end Date' Must Be Later Than 'start Date'

- 1 answer

I've created a form where users create an event the form includes a a date/time field. The input type allows users to select from calendar drop down.

Users must select a start date and and end date in the form.

If, for example, a user sets a start date of 01/04/2020 and I want the end date to automatically open at that 01/04/2020 as the end date will obviously be later than the start date. Is there a good way to do this?

Also assume i'll need to add some sort of separate validation in my control so that end date is always later than start date..?

addEvent.blade.php

  <div class="form-group">
  <div class="form-group">
    {!! Form::label('start_date', 'Start Date:') !!}
    <div class="">
      {!! Form::date('start_date', null, ['class' => 'form-control']) !!}
      {!! $errors->first('start_date', '<p class="alert alert-danger">:message</p>') !!}
    </div>
  </div>
</div>


<div class="form-group">
  <div class="form-group">
    {!! Form::label('end_date', 'End Date:') !!}
    <div class="">
      {!! Form::date('end_date', null, ['class' => 'form-control']) !!}
      {!! $errors->first('end_date', '<p class="alert alert-danger">:message</p>') !!}
    </div>
  </div>
</div>

AddEventController

public function addEvent(Request $request)
{
  $this->validate($request, [
    'event_name'  => 'required',
    'start_date'  => 'required',
    'end_date'    => 'required',
    'time'        => 'required',
    'trip_id'     => 'required',
    'address'     => 'required',

  ]);

    $start_date = Carbon::parse($request['start_date'])->format('Y-m-d');
    $end_date = Carbon::parse($request['end_date'])->format('Y-m-d');

    $tripCheck = Trip::where('id', $request['trip_id'])
    ->whereDate('startdate', '<=', $start_date)
    ->whereDate('enddate', '>=', $start_date)

    ->whereDate('startdate', '<=', $end_date)
    ->whereDate('enddate', '>=', $end_date)
    ->first();

    if ($tripCheck) {

      $events = new Events;
      $trips = Trip::all();
      $categories = Categories::pluck('category','id');
      $events->category_id = $request['category_id'];
      $events->colour =  $request['colour'];
      $events->event_name = $request['event_name'];
      $events->start_date = $request['start_date'];
      $events->end_date = $request['end_date'];
      $events->time = $request['time'];
      $events->address = $request['address'];
      $events->notes = $request['notes'];
      $events->trip_id = $request['trip_id'];
      $events->save();

//return redirect('trips')->with('success', 'The new event has been added to your trip')->with('trips', $trips)->withCategories($categories);
return redirect('trips/'.$request->input('trip_id').'/edit')->with('success', 'The new event has been added to your trip')->with('trips', $trips)->withCategories($categories);
} else
  {
  return redirect('trips')->withErrors(['The dates you added are not within Trip start and end date.']);
   }

}

Ad

Answer

You may use after or after_or_equal validations rule:

'start_date'  => 'required|date',
'end_date'    => 'required|date|after:start_date',

You may also use date_format validation rule to match the given format:

'start_date'  => 'required|date_format:m/d/Y',
'end_date'    => 'required|date_format:m/d/Y|after:start_date',
Ad
source: stackoverflow.com
Ad