0
0
Laravelframework~15 mins

Validate method on request in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
Validate method on request
📖 Scenario: You are building a simple Laravel web application where users submit a form with their name and email. You want to make sure the data is valid before saving it.
🎯 Goal: Create a Laravel controller method that uses the validate method on the request to check that the name is required and at least 3 characters long, and the email is required and a valid email address.
📋 What You'll Learn
Create a controller method named store that accepts a Request object
Use the validate method on the $request to validate name and email
The name field must be required and have a minimum length of 3
The email field must be required and be a valid email
Return a simple success message after validation
💡 Why This Matters
🌍 Real World
Validating user input is essential in web applications to prevent bad data and security issues.
💼 Career
Laravel developers frequently use the validate method on requests to enforce data rules before processing.
Progress0 / 4 steps
1
Create the controller method with Request parameter
Create a public method called store inside a controller that accepts a Request parameter named $request.
Laravel
Need a hint?

Remember to declare the method as public and accept Request $request as the parameter.

2
Add validation rules using the validate method
Inside the store method, use $request->validate() to validate that name is required and minimum 3 characters, and email is required and a valid email.
Laravel
Need a hint?

Use an array inside validate() with keys 'name' and 'email' and their validation rules.

3
Add a success response after validation
After the $request->validate() call, return a string 'Validation passed!' from the store method.
Laravel
Need a hint?

Simply return the string after validation to confirm success.

4
Complete the controller with namespace and use statements
Ensure the controller file starts with the correct namespace App\Http\Controllers; and includes use Illuminate\Http\Request; at the top.
Laravel
Need a hint?

The namespace and use statements must be at the top of the file before the class.