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
<?php
namespace App\Providers;
use Illuminate\Validation\Validator;
class CustomValidator extends Validator {
public function validateMethod($attribute, $value, $parameters)
{
return booleanValidation;
}
public function validateCaseDiff($attribute, $value, $parameters)
{
return preg_match(‘/([a-z]+.*[A-Z])|([A-Z]+.*[a-z])/’, $value);
}
public function validateLetters($attribute, $value, $parameters)
{
return preg_match(‘/[a-zA-Z]/’, $value);
}
public function validateNumbers($attribute, $value, $parameters)
{
return preg_match(‘/\d/’, $value);
}
For the VariableCheck line, I added the following: case_diff|numbers|letters
The code to include all the validation methods becomes:
‘VariableCheck’ => ‘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”,