0

i am trying to validate that the end date must be after the start date but its not working if i put the end date before the start date it pass without showing the error message here's my code

my view

<input id="start_date" type="datetime-local" class="form-control   @error('start_date') is-invalid @enderror" name="start_date"  required autocomplete="start_date" autofocus>

my controller

$this -> validate($request ,[

            'start_date' => [ Rule::unique('reservations')->where(function($query) use ($request) {
                $query->where('pitch_id', 'LIKE','%'.$request->pitch_id.'%');
              })],
              'end_date'=> 'required,datetime,before:start_date',
            ]);
STA
  • 30,729
  • 8
  • 45
  • 59
yassin
  • 71
  • 1
  • 1
  • 9
  • 1
    Does this answer your question? [Laravel 5.3 date validator: equal to or after start\_date](https://stackoverflow.com/questions/41342818/laravel-5-3-date-validator-equal-to-or-after-start-date) – Muhaddis Jun 12 '20 at 17:04
  • tried it but no – yassin Jun 12 '20 at 17:13
  • Whats the output of? `dd(Rule::unique('reservations')->where(function($query) use ($request) { $query->where('pitch_id', 'LIKE','%'.$request->pitch_id.'%'))`... ??? – STA Jun 12 '20 at 17:23
  • it worked, it was a sytax error – yassin Jun 12 '20 at 17:28

2 Answers2

1

Use after instead of before.

$this -> validate($request ,[

            'start_date' => [ Rule::unique('reservations')->where(function($query) use ($request) {
                $query->where('pitch_id', 'LIKE','%'.$request->pitch_id.'%');
              })],
              'end_date'=> 'required,datetime,after:start_date',
            ]);
Fanan Dala
  • 584
  • 6
  • 19
  • ` 'end_date'=> ['required','date','after_or_equal:start_date'],` tried this but still dont work – yassin Jun 12 '20 at 17:13
0
'start_date' => [ Rule::unique('reservations')->where(function($query) use ($request) {
    $query->where('pitch_id', 'LIKE','%'.$request->pitch_id.'%');
  })],
  'end_date'=> ['required','date','after:start_date'],

]);

now it works but the error message not showing

<div class="col-md-5">
              <input type="datetime-local" class="form-control " name="end_date"  id="end_date" required>
              @error('end_date')
              <span class="invalid-feedback " role="alert">
                  <strong>{{ $message }}</strong>
              </span>
          @enderror


             </div>

my custom

'custom' => [

        'end_date' => [
            'after:start_date' => 'la date de fin doit etre apres la date du debut',
        ],
    ],
yassin
  • 71
  • 1
  • 1
  • 9