Validate CPF in Laravel’s Registercontroller class

Asked

Viewed 491 times

1

I downloaded the package https://github.com/LaravelLegends/pt-br-validator to perform Cpf validation on my system. I followed the documentation instructions by putting Validatorprovider in the app.php.

In my registration system, I would like only valid numbers to be saved. However, I did not understand how to proceed with this validation in Registercontroller. I tried as follows:

protected function validator(array $data)
{
    return Validator::make($data, [
        'cpf' => 'formato_cpf',
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
    ]);
}

But it didn’t work. As much as I put an invalid Cpf I can still do the registration on the system. What I’m doing wrong?

  • formato_cpf is for format, and Cpf is the validation that checks the numbers ... you are not confusing.?

  • When I asked the question I was already a little desperate because I used 'Cpf' and it didn’t work, so I had tried 'formato_cpf', obviously unsuccessfully too. However, today I tested with 'Cpf' and finally the validation worked. I don’t understand what happened the first time, maybe it was necessary to restart the server or the Debian had not mapped the Laravel Legends namespace. I’ll never know, but now it’s all working.

1 answer

1


When I asked the question I was already a little desperate because I used 'Cpf' and it didn’t work, so I had tried 'formato_cpf', obviously unsuccessfully too. However, today I tested with 'Cpf' and finally the validation worked. I don’t understand what happened the first time, maybe it was necessary to restart the server or the Debian had not mapped the Laravel Legends namespace. I’ll never know, but now it’s all working.

Browser other questions tagged

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