Propaganistas / Laravel-Phone

Phone number functionality for Laravel
https://laravel-phone.herokuapp.com/
MIT License
2.75k stars 218 forks source link

manually validation #72

Closed fico7489 closed 6 years ago

fico7489 commented 6 years ago

Can I validate number manually, I don't see this in the documentation?

Why this is false

dd(PhoneNumber::isValidFormat('+17025891156'));

?

ghost commented 6 years ago

Same issue , always false

Propaganistas commented 6 years ago

Guys, a general advice: if you use undocumented methods like isValidFormat() always review the source code to check the actual functionality.

isValidFormat() merely checks if the supplied number format is valid (e.g. international, E.164, ...), it is NOT meant for phone number validation. So passing a phone number will always return false.

To validate a phone number manually you can just manually create Laravel's built-in Validator. For example:

use Validator;

$phoneNumber = '+17025891156';
$validator = Validator::make(['data' => $phoneNumber], ['data' => 'phone:US']);
$validator->passes(); // TRUE if phone number is a US number

If you don't want to use the Validator, then you can directly interact with libphonenumber and you don't need this package.

fico7489 commented 6 years ago

I don't want to use undocumented methods but there is no documented method for manual validation, however, I found I a workaround :

try {
    $phone = PhoneNumber::make($value)->getCountry();
    //valid
} catch (NumberParseException $e) {
    //not valid
}
Propaganistas commented 6 years ago

There's no need to document it as there isn't any dedicated method. You just need to leverage Laravel's Validator class. I'd highly encourage to switch over to my posted snippet instead of relying on your try/catch block. You're missing out edge cases like that.

fico7489 commented 6 years ago

I will try this, thanks for the response...