Discover how to keep your code neat and error-free by moving validation out of controllers!
Why Form request classes in Laravel? - Purpose & Use Cases
Imagine building a web form where users submit data, and you manually check each input for errors in your controller code.
You write many lines to validate data, handle mistakes, and keep your controller messy.
Manual validation clutters your controller, making it hard to read and maintain.
It's easy to forget checks or repeat code, causing bugs and security risks.
Fixing or updating validation means hunting through scattered code.
Form request classes let you move validation logic into a dedicated place.
This keeps controllers clean and validation reusable.
Laravel automatically runs these checks before your controller code, so you only handle valid data.
public function store(Request $request) { if (!$request->has('email') || !filter_var($request->email, FILTER_VALIDATE_EMAIL)) { return back()->withErrors(['email' => 'Invalid email']); } // save data }public function store(UserRequest $request) { // data is already validated here // save data }You can write cleaner, safer, and more organized code that scales well as your app grows.
When building a signup form, form request classes ensure emails, passwords, and names meet rules without cluttering your main logic.
Manual validation clutters and complicates controllers.
Form request classes centralize and automate validation.
This leads to cleaner, safer, and easier-to-maintain code.
Practice
Form Request class in Laravel?Solution
Step 1: Understand the role of Form Request classes
Form Request classes are designed to keep validation rules and authorization logic separate from controller code.Step 2: Identify the main benefit
This separation helps keep controllers clean and improves code organization and security.Final Answer:
To organize and handle validation rules separately from controllers -> Option DQuick Check:
Form Request = Validation organization [OK]
- Confusing Form Requests with database migrations
- Thinking Form Requests handle authentication
- Assuming Form Requests define routes
Solution
Step 1: Recall the Artisan command for Form Requests
The correct Artisan command to create a Form Request class ismake:request.Step 2: Match the command with the options
php artisan make:request UserRequest usesphp artisan make:request UserRequest, which is the correct syntax.Final Answer:
php artisan make:request UserRequest -> Option CQuick Check:
Artisan command for Form Request = make:request [OK]
- Using 'make:formrequest' instead of 'make:request'
- Using 'create:request' which is invalid
- Using 'generate:form' which does not exist
$request->validated() return if the input is ['name' => 'John', 'age' => 20]?
class UserRequest extends FormRequest {
public function rules() {
return [
'name' => 'required|string',
'age' => 'required|integer|min:18'
];
}
}Solution
Step 1: Check validation rules against input
The rules require 'name' as a string and 'age' as an integer >= 18. Input has 'name' = 'John' (string) and 'age' = 20 (integer, >=18), so validation passes.Step 2: Understand what validated() returns
Thevalidated()method returns all input fields that passed validation, so it returns the full input array.Final Answer:
{'name' => 'John', 'age' => 20} -> Option AQuick Check:
Valid input returns full validated data [OK]
- Assuming partial data is returned
- Expecting an error when input is valid
- Confusing validated() with all() method
class ProductRequest extends FormRequest {
public function rules() {
return [
'price' => 'required|numeric|min:0'
];
}
public function authorize() {
return false;
}
}Solution
Step 1: Understand the authorize() method role
Theauthorize()method controls if the request is allowed. Returning false denies the request before validation.Step 2: Effect of returning false in authorize()
Whenauthorize()returns false, Laravel automatically denies the request, so validation rules are not applied and the request fails authorization.Final Answer:
The authorize method returns false, blocking validation -> Option AQuick Check:
authorize() false blocks request [OK]
- Ignoring authorize() method effect
- Thinking min:0 is invalid for numeric
- Believing class name affects validation
email field but only if it is present, and it must be a valid email format. Which rule should you use in the rules() method?Solution
Step 1: Understand optional validation rules
To validate a field only if it is present in the input, Laravel uses thesometimesrule.Step 2: Compare rules for optional email
nullableallows null values and skips subsequent rules if null;presentrequires the field to exist even if empty;sometimesapplies validation only if the field is sent.Final Answer:
'email' => 'sometimes|email' -> Option BQuick Check:
Use sometimes for optional fields [OK]
- Using required which forces presence
- Using nullable which requires field present but can be null
- Using present which requires field always present
Ready to prove it?
Pass the test and this pattern moves unknown
