Home > Mobile >  Laravel - Is it possible to validate a get wildcard Request with Illuminate\Foundation\Http\FormR
Laravel - Is it possible to validate a get wildcard Request with Illuminate\Foundation\Http\FormR

Time:05-08

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.

CodePudding user response:

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.

CodePudding user response:

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;
}

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.

  • Related