Table of Contents In Laravel, many-to-many relationships are established using an intermediate table that holds foreign keys from the related
Boost Laravel Form Validation with Custom Form Request Classes
- Muhammad Waqas
- May 15, 2023
- 5:06 am
- No Comments
Table of Contents
Laravel is one of the most popular PHP frameworks for web development. It provides developers with a range of tools to build robust and scalable web applications. One such tool is the Custom Form Request Class, which allows developers to validate incoming requests before they are processed by the application. In this blog post, we will explore what Custom Form Request Classes are, how to use them, and provide three examples with proper class code and namespace.
What is a Custom Form Request Class?
A Custom Form Request Class is a class in Laravel that allows developers to define validation rules for incoming requests. Laravel provides a range of validation rules that can be used to validate input data, such as required fields, minimum and maximum lengths, and email formats. These rules can be defined in a Custom Form Request Class, which can then be used to validate incoming requests.
How to use a Custom Form Request Class?
To use a Custom Form Request Class in Laravel, you need to define a new class that extends the Illuminate\Foundation\Http\FormRequest class. This class provides a range of methods for defining validation rules, custom error messages, and more. Once you have defined your Custom Form Request Class, you can use it in your application by type-hinting it in your controller methods. When a request is received, Laravel will automatically validate the input data using the rules defined in your Custom Form Request Class.
Example 1: User registration form
Suppose you have a user registration form that requires users to provide a name, email, and password. You can create a Custom Form Request Class to validate this data, as follows:
php artisan make:request UserRegistrationRequest
This command will create a new file called UserRegistrationRequest.php
in the app/Http/Requests
directory.
'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8|confirmed',
];
}
}
In this example, we define validation rules for the name
, email
, and password
fields. The name
field is required and must be a string with a maximum length of 255 characters. The email
field is also required and must be a valid email address that is not already in use by another user. Finally, the password
field is required, must be a string with a minimum length of 8 characters, and must be confirmed by a matching password_confirmation
field.
Example 2: Contact form
Suppose you have a contact form on your website that allows visitors to send a message to your email address. You can create a Custom Form Request Class to validate this data, as follows:
php artisan make:request ContactFormRequest
This command will create a new file called ContactFormRequest.php
in the app/Http/Requests
directory.
'required|string|max:255',
'email' => 'required|string|email|max:255',
'message' => 'required|string|max:500',
];
}
}
In this example, we define validation rules for the name
, email
, and message
fields. The name
and email
fields are required and must be strings with a maximum length of 255 characters. The email
field must also be a valid email address. Finally, the message
field is required and must be a string with a maximum length of 500 characters.
Example 3: Update profile form
Suppose you have a form that allows users to update their profile information, including their name, email, and password. You can create a Custom Form Request Class to validate this data, as follows:
php artisan make:request UpdateProfileRequest
This command will create a new file called UpdateProfileRequest.php
in the app/Http/Requests
directory.
'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,' . $this->user()->id,
'password' => 'nullable|string|min:8|confirmed',
];
}
}
In this example, we define validation rules for the name
, email
, and password
fields. The name
and email
fields are required and must be strings with a maximum length of 255 characters. The email
field must also be a valid email address and must be unique among all user records in the database, except for the currently authenticated user. This is achieved by using $this->user()->id
in the unique rule, which excludes the current user’s email address from the check. Finally, the password
field is optional and must be a string with a minimum length of 8 characters, and must be confirmed by a matching password_confirmation
field.
Conclusion
Custom Form Request Classes are a powerful tool in Laravel that allow developers to validate incoming requests easily. They provide a convenient way to define validation rules for specific forms or requests, which can help improve the security and usability of your application. In this blog post, we have provided three examples of how to use Custom Form Request Classes, along with proper class code and namespaces. By using these examples as a starting point, you can create your own Custom Form Request Classes to validate any form or request in your Laravel application.
Popular Post
Table of Contents Introduction: Laravel is a PHP framework that has become quite popular in recent years. One of the
Table of Contents In Laravel, models are the backbone of the application. They act as a bridge between the database
Table of Contents Laravel is a popular PHP framework that is widely used for web application development. One of the
An experienced Code Debugger with a strong track record of identifying and resolving complex software issues. I aim to contribute my analytical skills and expertise in debugging tools to ensure efficient and reliable software operations and deliver high-quality software products that meet customer requirements.
If you found this content valuable, please show your support by following me on Medium, LinkedIn, Twitter and GitHub. Your support will motivate me to create more informative content in the future. Don’t forget to give a clap or share this blog with others if you found it helpful!