Custom Validation Rules in Laravel 5.5
Tags: laravel, validation, php
Published: 2020-01-03 17:01:37

Custom validation rules introduced in Laravel 5.5. Laravel custom validation makes it possible to add validation rules that are not defined in laravel.

In this article, we will try to determine if a phone number is valid or not using laravel custom validation rules.

Get started by creating the validation rule using the following artisan command:
```
php artisan make:rule PhoneNumberValidationRule
```
The above command should create a file called **PhoneNumberValidationRule.php** in the app/Rules folder.

By default, the artisan command will generate the following code snippet for you.
```php
namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class PhoneNumberValidationRule implements Rule
{

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        //
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The validation error message.';
    }
}
```
Note that, the class **PhoneNumberValidationRule** has two methods: passes and message.

However, the **passes** method receives the **$attribute** and **$value** arguments from the Laravel Validator. In short, **$attribute** is the field under validation, while $value as the variable suggests is the value of the field. The **passes** method returns true or false to determine the validity of the given value.

To validate our phone number, update the passes method with the code below:
```php
public function passes($attribute, $value)
{
     return preg_match('/^\d{10}$/',$value);
}
```
In addition, the **message** return the error message define when validation fails. Of course, update the message method with the code below:
```php
public function message()
{
    return ':attribute should be a valid phone number.';
}
```
## Using the Validation Rule

Finally, use the custom rule in the Controller during request. On the other hand, simply instantiate the custom validation class in the array rules in order to assign the rule to an attribute. For instance, use the validation method from the Request object in Laravel 5.5
```php
use App\Rules\PhoneNumberValidationRule;

public function store(Request $request)
{
    $this->validate($request, [
        'phone_number' => [new PhoneNumberValidationRule]
    ]);
}
```
Of course, if you prefer using as a Closure, the function looks like the following:
```php
public function store()
{
    $this->validate(request(), [
        'phone_number' => [function ($attribute, $value, $fail) {
            if ($value <= 10) {
                $fail(':attribute should be a valid phone number.');
            }
        }]
    ]);
}
```
Further, if you want the rule empty field in the request, then, use the ImplicitRule contract. Use the Closure to experiment with the custom rule and then change to an object if the rule proves successful.

```php
use Illuminate\Contracts\Validation\ImplicitRule;

class PhoneNumberValidationRule implements ImplicitRule
{
    public function passes($attribute, $value)
    {
        return preg_match('/^\d{10}$/',$value);
    }

    public function message()
    {
        return ':attribute should be a valid phone number.';
    }
}
```
In conclusion, this new feature in Laravel 5.5, is an easy way to define custom validation rule. Enjoy using this feature in your code.