As a Laravel developer, ensuring that user inputs are valid is crucial for building secure and reliable applications. In this article, I'll walk you through the process of validating email addresses, phone numbers, and passwords in Laravel 12.
We'll explore both basic and advanced validation techniques, including the use of Form Request classes for cleaner code.
validate()
MethodLaravel provides a straightforward way to validate incoming request data using the validate()
method. Here's how you can implement basic validation for email, phone, and password fields.
public function store(Request $request)
{
$validatedData = $request->validate([
'email' => 'required|email|unique:users,email',
'phone' => 'required|digits:10',
'password' => 'required|min:8|confirmed',
]);
}
Explanation:
required
: Ensures the field is not empty.
email
: Validates that the input is a valid email format.
unique:users,email
: Checks that the email is unique in the users table.
digits:10
: Ensures the phone number is exactly 10 digits.
min:8
: Password must be at least 8 characters long.
confirmed
: Requires a matching password_confirmation
field
For more complex applications, it's advisable to use Form Request classes to handle validation logic. This approach keeps your controller methods clean and promotes reusability.
Step 1: Create a Form Request Class
php artisan make:request StoreUserRequest
Step 2: Define Validation Rules
In app/Http/Requests/StoreUserRequest.php
public function rules()
{
return [
'email' => 'required|email|unique:users,email',
'phone' => 'required|digits:10',
'password' => 'required|min:8|confirmed',
];
}
Step 3: Use the Form Request in Controller
public function store(StoreUserRequest $request)
{
// Validation is automatically handled
// Proceed with storing the user
}
Benefits:
Keeps controller methods concise.
Centralizes validation logic.
Facilitates reuse of validation rules.
Sometimes, built-in validation rules may not suffice. Laravel allows you to create custom validation rules to handle specific scenarios.
Example: Prohibit Specific Usernames
Step 1: Create a Custom Rule
php artisan make:rule ProhibitedUsername
Step 2: Define the Rule Logic
In app/Rules/ProhibitedUsername.php
public function passes($attribute, $value)
{
return !in_array(strtolower($value), ['admin', 'superuser']);
}
public function message()
{
return 'The :attribute is prohibited.';
}
Step 3: Apply the Custom Rule
use App\Rules\ProhibitedUsername;
$request->validate([
'username' => ['required', new ProhibitedUsername],
]);
Validating phone numbers can be more complex due to varying formats. For Indian phone numbers, you can use a regular expression.
'phone' => ['required', 'regex:/^[6-9]\d{9}$/'],
Explanation:
regex:/^[6-9]\d{9}$/
: Ensures the number starts with 6-9 and is followed by 9 digits, totaling 10 digits.
To ensure users correctly confirm their passwords during registration, use the confirmed rule.
'password' => 'required|min:8|confirmed',
Validating user input is a fundamental aspect of building secure Laravel applications. By leveraging Laravel 12's validation features, including Form Request classes and custom rules, you can ensure data integrity and enhance user experience.
For more detailed information, refer to the official Laravel documentation on validation.
You might also like: