Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chaeril's avatar

Calling FormRequest validation manually

hi, usually we would use StoreCityRequest $request then call $request->validated() or calling validator manually like $this->validate($request, []);.

but i wanted to use something like Request $request then inside the function i'd call StoreCityRequest::validate($request).

we used a single function for store and update since the controller is already cramped and I wanted to make It cleaner

0 likes
9 replies
Chaeril's avatar

@Nakov I got error too few arguments

Too few arguments to function App\Http\Requests\StoreCityRequest::Illuminate\Foundation\Providers\{closure}(), 0 passed in d:\laravel-cms\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php on line 124 and exactly 1 expected 
Snapey's avatar

but if you pass StoreCityRequest $request into your controller, validation has already been done

why do it again?

Chaeril's avatar

@Snapey I pass Request $request, then inside the function, I (we) check if $id was null then its an insert. else it's an update. now both are validated using manual $this->validate() which grow bigger as we add more check and messages to it.

in the end I used $request->validate((new StoreCityRequest())->rules()); which was also found in the link @nakov shared

tykus's avatar

@Chaeril You've completely missed the point of FormRequests if you are not typehinting the FormRequest class in your Controller action(s).

public function store(StoreCityRequest $request)
{
   // you're here because validation and authorization is already done!
   City::create($request->validated());
   return redirect()->route('cities.index');
}
Snapey's avatar

@Chaeril You should be varying the rules in the formrequest class - or not using it at all.

Chaeril's avatar

@tykus I do understand it, but the boss wants me to use things that have already been made, instead of making things my way.

		// route looks like this.
        Route::get('/', 'CityController@index');
        Route::post('/', 'CityController@search');
        Route::get('/create', 'CityController@form')->name('city.create');
        Route::post('/create', 'CityController@save');
        Route::get('/update/{id}', 'CityController@form')->name('city.update');
        Route::post('/update/{id}', 'CityController@save');
        Route::get('/read/{id}', 'CityController@read');
        Route::get('/delete/{id}', 'CityController@delete');
        Route::get('/data', 'CityController@autocomplete');

their implementation was probably the same since L5. they used the same function for editing and creating. i tried my best creating a clean laravel way here... this one is trying to move $this->validate(['some' => 'rules']) in the Controller to StoreCityRequest and UpdateCityRequest.

tykus's avatar

@Chaeril there is nothing to prevent you reusing the same controller action for both storing and updating a record; you can still typehint the FormRequest class in the method signature and have validation done in the conventional way by the framework. The FormRequest class makes very little sense when used in the way you are suggesting; it does not require a class at all.

but the boss wants me to use things that have already been made,

You should consider finding a new boss IMO.

1 like

Please or to participate in this conversation.