Propaganistas/Laravel-Phone

Validating phone numbers from a database (model)

fueguino84 opened this issue · 3 comments

Hi, I'm using this library to format phone numbers on the views, but input data is not validated, so this can come to Exception errors.

I want is to check the data before sending it to the view, within the controller or ideally in the model. Is there a isValidNumber() or similar function available, or another way to accomplish this?

I tried adding use Illuminate\Support\Facades\Validator; at my model and this code
$validator = Validator::make(['number' => $data->number],['number' => 'phone:AUTO,AR']); if ($validator->fails()) die('error'); else die('paso');

But I get this error: Method [validatePhone] does not exist. So I think phone validation is not available within the model.

Any suggestion on how to validate these phone numbers coming from my database tithin the model?

Validating the database's phone number as you described should work fine.
By looking at the error message I suspect that the package's service provider didn't register (yet?). Are you sure that the application has fully booted before calling that validator?

Thank you, it worked after adding Propaganistas\LaravelPhone\PhoneServiceProvider::class to app.php. I thought it wasn't neccesary.

It should register automatically if you're on Laravel 5.5+.
Running php artisan package:discover rediscovers everything.

if that doesn't work out then there's something uncommon in your environment.
But either way manually registering the provider works equally well of course.