0

I have a GET route with a wildcard day.

This day wildcard is a string like so: 20220507 (YYYYMMDD).

After validation the string I wish to make a proper response. Before sending the response I want to validate the string length and the format.

My question is, is it possible to validate the string with Illuminate\Foundation\Http\FormRequest ou Illuminate\Http\Request make:request ? Or they only accepet Post Requests ?

Code:

php artisan make:request CalendarDayRequest

Example get route in web.php

Route::get('/calendar/{day}' , 'App\Http\Controllers\HomeController@calendar')->name('calendar');

Example Controller

use App\Http\Requests\CalendarDayRequest; 
public function calendar ( CalendarDayRequest $request ) {
    // Code
}

Or Example Controller 2

use Illuminate\Http\Request;

public function calendar ( Request $request ) {
    $validated = $request->validate([
        'day' => 'required',
     ]);
}

Both of them I got the error: infinite redirect loop, redirected it too many times.

rocket_moon
  • 309
  • 4
  • 18

2 Answers2

1

Firstly, you can not validate route parameters inside Form Request

But, you can use regex for validating your route Example:

Route::get('/calendar/{day}', 'App\Http\Controllers\HomeController@calendar')
    ->name('calendar')
    ->where('day', '/^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$/');

If You still want to use validate() function or Form Request

$request->merge([
   'day' => $day
]);

$this->validate($request, [
   'day' => 'date_format:Ymd',
]);

Form Request Way - Override all() method

public function all($k = null){
   $data = parent::all($k);
   $data['day'] = $this->route('day');
   return $data;
}

If Nothing Works, Try Following code

public function calendar(Request $request, $day){
 $data = $request->all();
 $data['day'] = $day; 

 $validator = Validator::make($data, [
   'day' => 'required|date_format:Ymd',
 ]);
  
 if($validator->fails()){
    // Do Something abort(404);
 }

}

Laravel 5 how to validate route parameters?

But I would suggest you to send it as request parameter if you want to use validate() function.

Anshul Kumar
  • 109
  • 4
  • For some reason the ```->where('day', '/^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$/');``` is not working, giving me 404 . – rocket_moon May 08 '22 at 08:51
  • The same problem happens with the validation fails with ```Form Request Way - Override all() method``` , infinity loop : too many redirects – rocket_moon May 08 '22 at 09:44
  • in `public function calendar ( Request $request, $day ) { $request->merge(['day' => $day ]); $request->validate([ 'day' => 'date_format:Ymd', ]); }` – Anshul Kumar May 08 '22 at 09:50
  • If Merge does'nt work try `$request->request->add(['day', $day]);` – Anshul Kumar May 08 '22 at 09:54
0

In Example 1:

You should overwrite this method in CalendarDayRequest:

protected function failedValidation(Validator $validator) {
    throw (new ValidationException($validator))
                ->errorBag($this->errorBag)
                ->redirectTo($this->getRedirectUrl());
}

If client expects json, laravel will return a json field instead of redirection. You can completely change this behavior by changing this method or customizing render() method in App\Exceptions\Handler.

In Example 2:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

public function calendar ( Request $request ) {
    $validator = Validator::make($request->all(), ['day'=>'required']);

    if ($validator->fails()) {
        $errors = $validator->errors();
        $messages = $validator->errors()->messages();
        $messageBag = $validator->errors()->getMessageBag();
    };
}

So you can use messages of validator for informing client.

mt3141
  • 1
  • 1