Laravel and Custom Validations

This tutorial shows how to extend Laravel validation with custom rules using provider-level validator methods and regular expressions. It includes a password-strength example and demonstrates how to add targeted custom error messages for better user feedback.

Coveros Staff

February 11, 2016

Problem with Default Validation

While using Laravel to build a web application, I needed to perform custom validation on user input, such as a password parameter. By default, Laravel manages this through AJAX handled on the client side. However, I wanted a more robust solution. Also, helpful libraries, such as a check of whether an input contains upper and lower case letters, were not included in the latest update of Laravel.

Illuminate’s validate method supports checks for size of a given variable and certain characteristics. There was no easily integrated custom solution.

The documentation for Laravel 5, specifically their validation docs, are located here: https://laravel.com/docs/5.1/validation

Below is the generic sample to checks two variables, variable1 and VariableCheck within the controller.

MethodController.php

/**

* Get a validator for an incoming registration request.

*

* @param  array  $data

* @return \Illuminate\Contracts\Validation\Validator

*/

public function validator(array $data) {

return Validator::make($data, [

‘variable1’ => ‘required|max:255’,

‘VariableCheck’ => ‘required|confirmed|min:6|method,

]);

}

The different checks enforce the qualities of the variables as entered by the user.

required – a value must be entered for the variable

min – defines minimum number of characters for entry

max – defines maximum number of characters for entry

confirmed – a matching field of foo_confirmation must exist. For example, if the field under validation is password, a matching password_confirmation field is required

method – similar to confirmed keyword, this signifies that a validateMethod is defined and will be called

This generic template was missing the necessary robust checks for characteristics such as a mix of letters, numbers, and upper and lower case values.

Solution for Custom Validation

To build the solution, I wrote simple regular expressions into the validation methods. The functions were added to PasswordValidator.php, which was placed in the Providers directory.

PasswordValidator.php

‘required|confirmed|min:6|method|case\_diff|numbers|letters ‘, Note that the comma at the end of the above statement is required within the code.   **Adding Custom Error Messages**   The default error message, “Whoops! There were some problems with your input,” is not helpful in explaining to users why they cannot proceed. By adding text within the resources\\lang\\en\\validation.php file, a much more aesthetically pleasing and specific error message may be used. I added this line to coincide with the validateCaseDiff function: “case\_diff” &=> “The :attribute must contain both upper and lower case characters”,

Coveros Staff

Coveros Staff

This post represents the collective insights of the Coveros team. Our staff consists of software experts who bring deep experience in secure agile development, DevOps, testing, and software quality. Over the past 20 years, Coveros has trained more than 30,000 professionals and worked with half of the Fortune 100 companies on mission-critical software development challenges. We draw on this extensive experience to share practical insights, proven strategies, and real-world solutions that help organizations build better software faster and more securely.