简体   繁体   中英

Laravel - Form select date - Only allow users to select between two dates send from database

I have two tables - 'Trips' and 'Events'. One trip can have many events. I have a form which allows users to add events to their trip. When selecting the start date and end date of the event I only want to allow the users to select a date that is from the trip start date and the trip end date.

For example if a trip to New York was created from the 1st March to the 14th March, users should only be able to add an event between those dates. Is there a good way to do this?

Trips Table Events Table

addEvent.blade.php `

<div class="form-group">
    <div class="form-group">
        {!! Form::label('trip_id', 'Your Destinations:') !!}
        <select class="form-control" name="trip_id">
          @foreach($trips as $trip)
            <option value='{{ $trip->id}}'>{{$trip->destination}}</option>
          @endforeach
        </select>
      </div>
    </div>

<div class="form-group">
  <div class="form-group">
    {!! Form::label('event_name', 'Add Event:') !!}
    <div class="">
      {!! Form::text('event_name', null, ['class' => 'form-control']) !!}
      {!! $errors->first('event_name', '<p class="alert alert-danger">:message</p>') !!}
    </div>
  </div>
</div>

<div class="form-group">
  <div class="form-group">
    {!! Form::label('start_date', 'Start Date:') !!}
    <div class="">
      {!! Form::date('start_date', \Carbon\Carbon::now(), ['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>

<div class="form-group" &nbsp;<br/>
{!! Form::submit('Add Event', ['class' => 'btn btn-primary']) !!}

`

Event Controller

public function addEvent(Request $request)
{
  $validator = Validator::make($request->all(),[
    'event_name' => 'required',
    'start_date' => 'required',
    'end_date' => 'required',
    'trip_id'=> 'required',
  ]);

  if($validator->fails()) {
    \Session::flash('warning', 'Please enter the valid details');
    return redirect('/events')->with('input', Input::all());
  }

  $events = new Events;
  $trips = Trip::all();
  $events->event_name = $request['event_name'];
  $events->start_date = $request['start_date'];
  $events->end_date = $request['end_date'];
  $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);
}

trip.blade.php

<div class="form-group">
      <input type="text" name="destination" class="form-control" value="{{$trip->destination}}" placeholder="Destination" />
    </div>
    <h7>Trip Start Date: </h7>
    <div class="form-group">
      <input type="date" name="startdate" class="form-control" value="{{$trip->startdate}}" placeholder="Start Date" />
    </div>
    <h7>Trip End Date: </h7>
    <div class="form-group">
      <input type="date" name="enddate" class="form-control" value="{{$trip->enddate}}" placeholder="End Date" />
    </div>
    <div>

trip controller

public function submit(Request $request){
  $this->validate($request, [
    'name'          => 'required',
    'email'         => 'required',
    'destination'   => 'required',
    'startdate'     => 'required',
    'enddate'       => 'required'
  ]);

  //Create new trips
  $trip = new Trip;
  $trip->name = $request->input('name');
  $trip->email = $request->input('email');
  $trip->destination = $request->input('destination');
  $trip->startdate = $request->input('startdate');
  $trip->enddate = $request->input('enddate');
  $trip->user_id = auth()->user()->id;

  //save trips
  $trip->save();

  //Rredirect
  return redirect('/home')->with('status', 'Trip Created Sucessfully');

}

in the addEvent.blade.php file you can add the "min" and "max" attributes to start_date and end_date based on existing trip dates

{!! Form::date('start_date', \Carbon\Carbon::now(), ['class' => 'form-control', 'min' => $trip->startdate, 'max' => $trip->enddate]) !!}


{!! Form::date('end_date', null, ['class' => 'form-control', 'min' => $trip->startdate, 'max' => $trip->enddate]) !!}

and in the addEvent function you have to add validations for start_date and end_date so that they do not exceed the dates of the trip table

public function addEvent(Request $request)
{

  // first take data from the trip table
  $trip = Trip::find($request->trip_id);

  // if trip not found then error
  if( !$trip ){
    return redirect('trips')->with('fail', 'Trip not found');
  }

  // add validations for start_date and end_date so that they do not exceed the dates of the trip table
  // gte is for grather than equal
  // lte is  for less than equal
  // or you can use min max

  $validator = Validator::make($request->all(),[
    'event_name' => 'required',
    'start_date' => 'required|date|min:'.$trip->startdate.'|max:'.$trip->enddate, //
    'end_date' => 'required|date|gte:start_date|max:'.$trip->enddate,
    'trip_id'=> 'required',
  ]);

  if($validator->fails()) {
    \Session::flash('warning', 'Please enter the valid details');
    return redirect('/events')->with('input', Input::all());
  }

  $events = new Events;
  $trips = Trip::all();
  $events->event_name = $request['event_name'];
  $events->start_date = $request['start_date'];
  $events->end_date = $request['end_date'];
  $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);
}
$start_date = Carbon::parse($request['start_date'])->formet('Y-m-d');
$end_date = Carbon::parse($request['end_date'])->formet('Y-m-d');

$tripCheck = Trip:where('id', $request['trip_id'])
->whereDate('start_date', '>=', $start_dat)
->whereDate('end_date', '<', $end_date)
->first();

$trips = Trip::all();
$status = failed;
$msg = 'The new event has been not added to your trip';
if($tripCheck){
    $events = new Events;
    $events->event_name = $request['event_name'];
    $events->start_date = $request['start_date'];
    $events->end_date = $request['end_date'];
    $events->trip_id = $request['trip_id'];
    $events->save();
    $status = 'success';
    $msg = 'The new event has been not added to your trip';
}

return redirect('trips')->with($status, $msg)->with('trips', $trips);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM