Customization of Laravel Validator message

Asked

Viewed 103 times

0

I have the following validator cpfValidacao.php:

<?php

namespace App\Utils;

class cpfValidacao
{
    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;
    }
}

Call on the AppServiceProvider.php:

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
   Validator::extend('cpf', '\App\Utils\cpfValidacao@validate');
}

Call on the controller:

 /* validação de parâmetros */
 $validator = Validator::make($request->all(), [
     'email' => 'required|min:1|max:255',
     'cpf' => 'required|min:1|max:11|cpf',
     'name' => 'required|min:1|max:255',
     'password' => 'required|min:8|max:255'
     //'profile_picture_path' => 'required|min:1|max:255'
 ]);
 if($validator->fails()){
     $errors = $validator->errors();
     return response()->json(['error' => true, $errors], 422);
 }
 /* fim */

Return:

{
    "error": true,
    "0": {
        "cpf": [
            "validation.cpf"
        ]
    }
}

I want to customize the message "validation.cpf". How do you do?

Laravel 6.2

  • The ideal would be to create a Rule even without having to extend the service Provider, there you can define the message you want, https://laravel.com/docs/7.x/validation#custom-validation-Rules

  • Right thanks brother

1 answer

2


As commented by Thiago, the ideal would be for you to create a Rule as follows: Running the command to create the validation file in app/rules

php artisan make:rule CpfValidation

In the passes function put your logic:

public function passes($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;
}

And in the messaging function, the message you return if you give validation error:

public function message()
{
    return 'Cpf inválido.';
}

An example of how to use validation done:

$validator = Validator::make($request->all(), [
 'email' => 'required|min:1|max:255',
 'cpf' => ['required', 'min:1', 'max:11', new CpfValidation],
 'name' => 'required|min:1|max:255',
 'password' => 'required|min:8|max:255'
 //'profile_picture_path' => 'required|min:1|max:255'
 ]);
  • Got it. I did it here and it already worked. It is possible to add more than one function in it "rules"?

  • 1

    @Luizrobertofurtuna otherwise has to do with the CPF is not legal to put together because there you are disrespecting the principle of sole responsibility, always try to keep things separate. so if you need another rule, just make another one again

Browser other questions tagged

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