Introduction to Laravel Form Validation

Form validation is a critical aspect of web applications, ensuring that data submitted by users adheres to predefined rules before it is processed or stored. Laravel’s form validation system streamlines this process, providing developers with an expressive syntax and a range of validation rules to safeguard against common input errors and security threats.
Creating a Form in Laravel
Before delving into validation, let’s briefly touch on creating a form in Laravel. Laravel offers a Blade templating engine that simplifies the integration of HTML and PHP. For instance, a basic form in Laravel might look like this:
<form method="POST" action="/submit-form"> csrf <label for="username">Username:</label> <input type="text" id="username" name="username" required> <label for="email">Email:</label> <input type="email" id="email" name="email" required> <button type="submit">Submit</button> </form>
In this example, the `csrf` directive generates a CSRF token to protect against cross-site request forgery attacks.
Laravel Validation Rules
Laravel provides an extensive set of validation rules, making it easy to define the specific criteria that user input must meet. Some common validation rules include:
- required: The field must be present in the input data.
- email: The field must be a valid email address.
- numeric: The field must be a numeric value.
- min: value: The field must be at least the specified minimum value.
These rules can be combined and customized to suit the unique requirements of each form field.
Performing Validation in Laravel Controllers

When a form is submitted, the input data is typically sent to a controller for processing. Laravel’s validation can be seamlessly integrated into controllers using the `validate` method. For example:
public function submitForm(Request $request)
{ $validatedData = $request->validate([ 'username' => 'required|string|max:255', 'email' => 'required|email', ]); // Process the validated data }
In this example, if the validation fails, Laravel will automatically redirect the user back to the form with error messages. The `$validatedData` variable will only contain the validated data if the validation is successful, allowing developers to proceed with confidence.
Displaying Validation Errors in Views
When validation fails, Laravel makes it simple to display error messages in the views. For each form field, you can use the `error` directive to check if there are validation errors:
<label for="username">Username:</label> <input type="text" id="username" name="username" required> @error('username') <div class="error">{{ $message }}</div> @enderror
This snippet checks if there are validation errors for the ‘username’ field and, if so, displays the error message.
Custom Error Messages and Localization
Laravel allows developers to customize error messages for each validation rule, making it easier to provide clear feedback to users. For instance:
$messages = [ 'username.required' => 'The username field is required.', 'email.required' => 'The email field is required.', 'email.email' => 'Please enter a valid email address.', ]; $request->validate([ 'username' => 'required|string|max:255', 'email' => 'required|email', ], $messages);
Additionally, Laravel supports localization, allowing you to provide error messages in multiple languages.
Conditional Validation Rules

In certain scenarios, you may need to apply validation rules conditionally. Laravel facilitates this by enabling developers to use the `sometimes` method within the validation closure. For example:
request->validate([ 'password' => 'required|string|min:8', 'password_confirmation' => 'required_with:password|same:password', ], [ 'password_confirmation.required_with' => 'Please confirm your password.', 'password_confirmation.same' => 'The password confirmation does not match.', ]);
In this example, the ‘password_confirmation’ field is validated only if the ‘password’ field is present.
Form Request Validation: A Structured Approach
For larger applications, Laravel encourages the use of form request validation, which involves creating a dedicated class to handle validation rules. These classes are generated using the Artisan command:
php artisan make:request SubmitFormRequest
Once created, the validation rules are defined within the generated form request class. This approach promotes code organization and reusability.
Concluding: Empowering Developers with Laravel Form Validation
In conclusion, Laravel’s form validation system serves as a powerful ally for developers working with user input in web applications. Its expressive syntax, extensive set of validation rules, and seamless integration into controllers and views streamline the process of handling user-submitted data.
Whether you’re building a simple contact form or a complex registration system, Laravel’s form validation empowers developers to enforce data integrity, enhance user experience, and fortify application security. By adopting Laravel’s conventions for form validation, developers can strike a balance between flexibility and structure, creating web applications that not only function seamlessly but also provide a polished and user-friendly experience. Happy validating!
Don't want to miss anything?
Get weekly updates on the newest design stories, case studies and tips right in your mailbox.