0
0
Laravelframework~10 mins

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

Choose your learning style9 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.