PASSWORD RESET

Your destination for complete Tech news

How to create a custom validation in Laravel?

503 0
2 min read

In Laravel, you can create custom validation rules by creating a custom validation rule class and registering it with the Validator facade.

Here’s an example of how you can create a custom validation rule to validate that a given value is a multiple of a given number:

1. First, create a new validation rule class by running the following Artisan command:

php artisan make:rule MultipleOf

This will create a new class in the app/Rules directory.

2. In the MultipleOf class, define the passes method, which receives the attribute being validated, the value being validated, and the parameters passed to the rule. This method should return a boolean value indicating whether the validation passed or failed.

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class MultipleOf implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        // Return true if the value is a multiple of the given number
        return $value % $this->number == 0;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The :attribute must be a multiple of :number.';
    }
}

3. To use the custom validation rule, pass an instance of the MultipleOf class to the Validator facade’s make method along with the data you want to validate. You can pass any additional parameters to the rule as an array.

use App\Rules\MultipleOf;
use Illuminate\Support\Facades\Validator;

$data = [
    'number' => 8,
];

$validator = Validator::make($data, [
    'number' => ['required', new MultipleOf(2)],
]);

if ($validator->fails()) {
    // Validation failed
} else {
    // Validation passed
}

In Laravel, you can create custom validation rules by creating a custom validation rule class and registering it with the Validator facade.

Here’s an example of how you can create a custom validation rule to validate that a given value is a multiple of a given number:

  1. First, create a new validation rule class by running the following Artisan command:
Copy codephp artisan make:rule MultipleOf

This will create a new class in the app/Rules directory.

  1. In the MultipleOf class, define the passes method, which receives the attribute being validated, the value being validated, and the parameters passed to the rule. This method should return a boolean value indicating whether the validation passed or failed.
Copy code<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class MultipleOf implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        // Return true if the value is a multiple of the given number
        return $value % $this->number == 0;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The :attribute must be a multiple of :number.';
    }
}
  1. To use the custom validation rule, pass an instance of the MultipleOf class to the Validator facade’s make method along with the data you want to validate. You can pass any additional parameters to the rule as an array.
Copy codeuse App\Rules\MultipleOf;
use Illuminate\Support\Facades\Validator;

$data = [
    'number' => 8,
];

$validator = Validator::make($data, [
    'number' => ['required', new MultipleOf(2)],
]);

if ($validator->fails()) {
    // Validation failed
} else {
    // Validation passed
}

You can also create a custom validation rule by extending the Illuminate\Validation\Validator class and using the extend method of the Validator facade to register it.

For more information about custom validation rules in Laravel, you can refer to the documentation at https://laravel.com/docs/8.x/validation#custom-validation-rules.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.