Laravel 5.8 - Create custom validations to use on a Formrequest?

Asked

Viewed 432 times

0

I would like to create some custom validations and call them inside the FormRequest as per image:

Regras do FormRequest

  • What validation? is more specific

  • in case it would be any kind of validation that does not come by default in Laravel. For example validate a "CPF", validate whether a specific word came in a given field. I would like to create several types of validations and be able to call them directly from Formrequest, as I call the native validations of Laravel.

1 answer

0


To create a validation in this way create a class to validate this process, which has the following code:

Verify Cnpj:

<?php namespace App\Utils;


class CnpjValidation
{
    public function validate($attribute, $value, $parameters, $validator)
    {
        return $this->isValidate($attribute, $value);
    }

    protected function isValidate($attribute, $value)
    {
        $c = preg_replace('/\D/', '', $value);
        $b = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
        if (strlen($c) != 14) {
            return false;
        }
        elseif (preg_match("/^{$c[0]}{14}$/", $c) > 0) {
            return false;
        }
        for ($i = 0, $n = 0; $i < 12; $n += $c[$i] * $b[++$i]);
        if ($c[12] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
            return false;
        }
        for ($i = 0, $n = 0; $i <= 12; $n += $c[$i] * $b[$i++]);
        if ($c[13] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
            return false;
        }
        return true;
    }
}

Verify Cpf

<?php namespace App\Utils;

class CpfValidation
{
    public function validate($attribute, $value, $parameters, $validator)
    {
        return $this->isValidate($attribute, $value);
    }

    protected function isValidate($attribute, $value)
    {
        $c = preg_replace('/\D/', '', $value);
        if (strlen($c) != 11 || preg_match("/^{$c[0]}{11}$/", $c)) {
            return false;
        }
        for ($s = 10, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
        if ($c[9] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
            return false;
        }
        for ($s = 11, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
        if ($c[10] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
            return false;
        }
        return true;
    }
}

in these two classes have two validations that usually do not have in the standard validations package and need to coexist.

To register these two classes open the way app\Providers\AppServiceProvider.php and within the method of that class boot put these two lines:

/*Validations*/
Validator::extend('cpf', '\App\Utils\CpfValidation@validate');
Validator::extend('cnpj', '\App\Utils\CnpjValidation@validate');

and with that in the classes FormRequest call the alias cpf and cnpj as an example below:

$rules = [
    'input_cnpj' => 'required|cnpj',
    ....

If the validation is small it does not suffer much change in this method boot a function can also be created in this way:

Validator::extend('cep', function($attribute, $value, $parameters, $validator) 
{
    return mb_strlen($value) === 8 && preg_match('/^(\d){8}$/', $value)
});

this validation is for cep and can also be used directly so.

References.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.