Bird
Raised Fist0
Laravelframework~10 mins

Form request classes in Laravel - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Form request classes
User submits form
↓
FormRequest class intercepts
↓
Validate input rules
↓
Controller
↓
Show errors
When a user submits a form, the FormRequest class checks input against rules. If valid, data goes to the controller. If invalid, user sees errors.
Execution Sample
Laravel
public function rules()
{
    return [
        'email' => 'required|email',
        'password' => 'required|min:8'
    ];
}
Defines validation rules for email and password fields in a FormRequest class.
Execution Table
StepActionInput DataValidation ResultNext Step
1Receive form data{email: 'user@example.com', password: 'secret123'}Not checked yetValidate input
2Check 'email' field'user@example.com'Passes 'required' and 'email' rulesCheck next field
3Check 'password' field'secret123'Passes 'required' and 'min:8' rulesAll fields valid
4All validations passedN/AValidSend data to controller
5Controller processes dataValid dataN/AComplete request
6If invalid input example{email: '', password: '123'}Fails 'email' required and 'password' min:8Redirect back with errors
💡 Validation stops when all rules pass or any rule fails, then proceeds accordingly.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
emailuser@example.comuser@example.comuser@example.comValid
passwordsecret123secret123secret123Valid
validation_statusNot checkedPartial passAll passValid
Key Moments - 2 Insights
Why does the form redirect back instead of calling the controller?
If any validation rule fails (see step 6 in execution_table), Laravel automatically redirects back with error messages instead of running controller code.
How does Laravel know which rules to apply?
The rules() method in the FormRequest class returns the validation rules array (shown in execution_sample), which Laravel uses to check each input field.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the validation result for the password field?
AFails 'required' rule
BPasses 'required' and 'min:8' rules
CFails 'min:8' rule
DNot checked yet
💡 Hint
Check the 'Validation Result' column at step 3 in the execution_table.
At which step does Laravel decide to send data to the controller?
AStep 2
BStep 6
CStep 4
DStep 1
💡 Hint
Look for the step where 'All validations passed' and 'Send data to controller' appear in the execution_table.
If the email field is empty, how does the execution_table change?
AStep 2 shows failure on 'email' required rule
BStep 3 shows failure on 'password' rule
CStep 4 sends data to controller anyway
DNo change, validation passes
💡 Hint
Refer to step 6 in execution_table where invalid input causes failure on 'email' required rule.
Concept Snapshot
Form Request Classes in Laravel:
- Create a FormRequest class to handle validation.
- Define rules() method returning validation rules.
- Laravel auto-validates input before controller runs.
- On failure, redirects back with errors.
- On success, controller receives validated data.
Full Transcript
When a user submits a form in Laravel, the FormRequest class intercepts the data. It runs the rules defined in the rules() method to check each input field. If all fields pass validation, the data is sent to the controller for processing. If any field fails, Laravel automatically redirects the user back to the form with error messages. This process helps keep controller code clean and validation centralized. The execution table shows each step: receiving data, checking each field, deciding if data is valid, and either continuing or redirecting. Variables like email and password hold input values and their validation status updates as checks run. Understanding this flow helps beginners see how Laravel manages form validation smoothly.

Practice

(1/5)
1. What is the main purpose of a Form Request class in Laravel?
easy
A. To define routes for the application
B. To create database migrations automatically
C. To manage user authentication sessions
D. To organize and handle validation rules separately from controllers

Solution

  1. 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.
  2. Step 2: Identify the main benefit

    This separation helps keep controllers clean and improves code organization and security.
  3. Final Answer:

    To organize and handle validation rules separately from controllers -> Option D
  4. Quick Check:

    Form Request = Validation organization [OK]
Hint: Form Requests separate validation from controllers [OK]
Common Mistakes:
  • Confusing Form Requests with database migrations
  • Thinking Form Requests handle authentication
  • Assuming Form Requests define routes
2. Which of the following is the correct way to create a Form Request class using Artisan CLI?
easy
A. php artisan create:request UserRequest
B. php artisan make:formrequest UserRequest
C. php artisan make:request UserRequest
D. php artisan generate:form UserRequest

Solution

  1. Step 1: Recall the Artisan command for Form Requests

    The correct Artisan command to create a Form Request class is make:request.
  2. Step 2: Match the command with the options

    php artisan make:request UserRequest uses php artisan make:request UserRequest, which is the correct syntax.
  3. Final Answer:

    php artisan make:request UserRequest -> Option C
  4. Quick Check:

    Artisan command for Form Request = make:request [OK]
Hint: Use 'make:request' to create Form Requests [OK]
Common Mistakes:
  • Using 'make:formrequest' instead of 'make:request'
  • Using 'create:request' which is invalid
  • Using 'generate:form' which does not exist
3. Given this Form Request class snippet, what will $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'
        ];
    }
}
medium
A. {'name' => 'John', 'age' => 20}
B. {'name' => 'John'}
C. ValidationException error
D. Empty array []

Solution

  1. 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.
  2. Step 2: Understand what validated() returns

    The validated() method returns all input fields that passed validation, so it returns the full input array.
  3. Final Answer:

    {'name' => 'John', 'age' => 20} -> Option A
  4. Quick Check:

    Valid input returns full validated data [OK]
Hint: Validated returns all fields passing rules [OK]
Common Mistakes:
  • Assuming partial data is returned
  • Expecting an error when input is valid
  • Confusing validated() with all() method
4. Identify the error in this Form Request class that causes validation to always pass even with invalid input:
class ProductRequest extends FormRequest {
    public function rules() {
        return [
            'price' => 'required|numeric|min:0'
        ];
    }

    public function authorize() {
        return false;
    }
}
medium
A. The authorize method returns false, blocking validation
B. The rules method is missing required fields
C. The min:0 rule is invalid for numeric fields
D. The class name should be ProductFormRequest

Solution

  1. Step 1: Understand the authorize() method role

    The authorize() method controls if the request is allowed. Returning false denies the request before validation.
  2. Step 2: Effect of returning false in authorize()

    When authorize() returns false, Laravel automatically denies the request, so validation rules are not applied and the request fails authorization.
  3. Final Answer:

    The authorize method returns false, blocking validation -> Option A
  4. Quick Check:

    authorize() false blocks request [OK]
Hint: authorize() must return true to run validation [OK]
Common Mistakes:
  • Ignoring authorize() method effect
  • Thinking min:0 is invalid for numeric
  • Believing class name affects validation
5. You want to create a Form Request that validates an optional 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?
hard
A. 'email' => 'nullable|email'
B. 'email' => 'sometimes|email'
C. 'email' => 'required|email'
D. 'email' => 'present|email'

Solution

  1. Step 1: Understand optional validation rules

    To validate a field only if it is present in the input, Laravel uses the sometimes rule.
  2. Step 2: Compare rules for optional email

    nullable allows null values and skips subsequent rules if null; present requires the field to exist even if empty; sometimes applies validation only if the field is sent.
  3. Final Answer:

    'email' => 'sometimes|email' -> Option B
  4. Quick Check:

    Use sometimes for optional fields [OK]
Hint: Use sometimes to validate only if field exists [OK]
Common Mistakes:
  • Using required which forces presence
  • Using nullable which requires field present but can be null
  • Using present which requires field always present
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard