propaganistas / laravel-phone Goto Github PK
View Code? Open in Web Editor NEWPhone number functionality for Laravel
Home Page: https://laravel-phone.herokuapp.com/
License: MIT License
Phone number functionality for Laravel
Home Page: https://laravel-phone.herokuapp.com/
License: MIT License
Hello,
When using the LaravelPhoneFacade or anytime I use PhoneNumberUtil::getInstace() in the consctructor of a Controller I'm getting a:
PHP Fatal error: Uncaught Error type: 1. The phone number supplied was null. thrown in PhoneNumberUtil.php on line 1376
This happens when I run artisan route:list for example.
Psy Shell v0.8.14 (PHP 7.1.11 — cli) by Justin Hileman
>>> $number = phone('447937947973', 'GB');
=> Propaganistas\LaravelPhone\PhoneNumber {#907}
>>> $number->getCountry();
Propaganistas\LaravelPhone\Exceptions\NumberParseException with message 'Country specification for number "447937947973" required.'
>>> $number = phone('12252280247', 'US');
=> Propaganistas\LaravelPhone\PhoneNumber {#935}
>>> $number->getCountry();
=> "US"
What am I doing wrong?
I try to validate this number +4989620603291873 but it fails, but the fax number is valid and exists.
Is there anything i can do, to get it validated.
Hi,
Please see:
xfxf@ec03230
I added a parameter called "LENIENT" to expose more lenient phone number validation (in our case, area codes being optional) in the library you're relying on.
This code works; we're using it on a project right now.
I see you've refactored parts of this since the commit I based this off; I'm happy to update the code to reflect a PR against your latest commit, but probably be good to get feedback from you first.
Hi,
I have a problem when receiving an empty value for a country code. The InvalidParameterException error is thrown but it should not be the case.
I think on line 114 of PhoneValidator.php, the line should be:
if (!is_null($this->isInputField($parameter))) {
Does that seem right?
Thanks.
Phone number for example: +375 25 260 55 12 - is a mobile number for Belarus. (https://countrycode.org/belarus)
(https://en.wikipedia.org/wiki/List_of_mobile_phone_number_series_by_country - please search on page +375)
Rules:
'phone' => 'required|phone:AUTO' - not working
'phone' => 'required|phone:AUTO,mobile' - not working
'phone' => 'required|phone:BY' - not working
'phone' => 'required|phone:BY,mobile' - not working
Oh sorry - all working fine (+375 25 260 55 12 - this is a bad number)
Thanks again for putting this out there.
Laravel 5.2 now supports validation arrays, it looks like the phone validator doesn't take advantage of that yet, I'd like to be able to do this:
'something.*.phone' => 'phone:something.*.phone_country'
Similar to what is shown in the docs:
$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users',
'person.*.first_name' => 'required_with:person.*.last_name',
]);
Hello,
I've used the validation rule perfectly while using the validation request inside a controller, but now that I'm moving said rules under their own requests object (artisan make:request
), the phone:Country
validation rule doesn't seems to work.
Is there anything I should do to make it work there?
I used to have the following validation rule:
'mobile' => 'phone:NL,mobile',
As of 2.7 this no longer works as you check if the 'mobile' parameter is the input field, which in this case it is, and starts to assume I will have a custom_country_field.
Of course easily fixed by changing the name of the form field to something other than mobile, but imo this still is a bug as the form field name should have no consequences.
Can I validate number manually, I don't see this in the documentation?
Why this is false
dd(PhoneNumber::isValidFormat('+17025891156'));
?
laravel-phone is awesome! Just a tiny suggestion: According to composer.json it depends on libphonenumber-for-php 5.9.*, which seems to equate to 5.9.4 from February in my installation. Can we keep up with 6.x versions (currently 6.1.1), or will laravel-phone seize to work with versions newer than 5.9?
I think there might be a problem with validation. I want to write validation for phone number ( for ex. +91 12345674890) and it works fine but the problem is whenever a user enters any two characters after a mobile number (for ex. +91(1234567891) bd ) it treats as a valid phone number. Please give me appropriate solution.
Thanks in advance.
Hi,
phone validation (phone:BE) fails with this number : +32 4 799 70 15
but works if I add a number at the end
or with those one : +32 4 199 70 15 - +32 4 299 70 15 - +32 4 399 70 15
to resume:
works if
fails if
Can you please correct this? :)
Thanks a lot,
pudeldoodle
I have the phone
attribute on User
model which currently use this package for phone formatting via Eloquent mutator and accessor like so
User
Model
public function getPhoneAttribute($value)
{
return phone($value)->formatForCountry('VN');
}
public function setPhoneAttribute($value)
{
return $this->attributes['phone'] = phone($value, 'VN');
}
While perfoming update request to the controller with no changes the phone attribute still getting updated with same attribute. This occur even if the getPhoneAttribute
method removed.
UserController
public function update(UpdateProfileRequest $request)
{
$request->user()->update($request->all());
return redirect()->back();
}
So it seem that the Model is know the phone attribute has been dirtied even if it is not?
I have the following code in laravel, but no matter what I do, it always throws an exception.
Exception:
NumberParseException in PhoneNumberUtil.php line 1348: Missing or invalid default region.
Route::get('/sms', function() {
return phone_format('2025550115');
});
Not an issue, but just wanted to say thanks for making this!
Hi there,
just discovered that the Validator doesn't handle nested validation rules like:
address.phone
address.phone_country
Flatening the Array in line 126 of PhoneValidator.php with:
$data = array_dot($validator->getData());
seems to fix the issue.
Maybe you can validate my findings and merge to master if correct.
Thanks,
volker
Hi, is there a possibility to validate without country (or guess country) having only full number (e.g. +12512512512)
I need to validate number, but I don't have country field.
For example this frontend libray https://github.com/Bluefieldscom/intl-tel-input uses Google LibPhoneNumber and could guess country by full number.
use Illuminate\Foundation\Application;
Lumen Illuminate\Foundation\Application No Found
Hi,
libphonenumber-for-php v8 has been released, and it'll be good for this project to support it going forwards.
The BC changes are:
ShortNumberUtil
, use ShortNumberInfo
insteadShortNumberInfo
that work on strings, insteadPhoneNumberUtil::parse
first to create aPhoneNumber
object, and pass this in.Hello,
Thank you for your work on this project. I have run into an issue when trying to integrate this with my site. When I attach the phone
rule to my form. It seems to just continually return that it's in error, though I'm using my personal phone number. Please let me know what information you need from me and I will do all I can to help.
Thanks!
Could you add a replacer so :value becomes available from the custom message?
$validator->addReplacer('phone', function ($message, $attribute, $rule, $parameters) use ($value) {
return str_replace(':value', $value, $message);
});
This allows:
"phone" => "The number :value is invalid.",
Thanks.
When adding in the phone validation to my rules I get the following exception:
BadMethodCallException in Validator.php line 2615:
Method [validatePhone] does not exist.
I can not figure out, why I do not get erros back, and also old input if form is not validated
If I leave empty phone field I get:
NumberParseException in PhoneNumberUtil.php line 1383:
The string supplied did not seem to be a phone number.
If I enter letters I get
NumberParseException in PhoneNumberUtil.php line 1383:
The string supplied did not seem to be a phone number.
<div class="col-md-6 pl-none">
<label for="phonefield" >Phone</label>
<input name="phonefield" id="phonefield" type="phone" class="form-control input-lg" {{ $errors->has('phonefield') ? ' autofocus' : '' }}>
@if ($errors->has('phonefield'))
<span class="help-block">
<strong>{{ $errors->first('phonefield') }}</strong>
</span>
@endif
</div>
protected function validator(array $data)
{
$phonefield = phone_format($data['phonefield'], $country_code = 'LV', $format = PhoneNumberFormat::E164);
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'phonefield' => 'phone:LV|unique:users',
]);
}
Also I added in validation.php
'phone' => 'The :attribute field contains an invalid number.',
What I am doing wrong?
What would be the correct way to store phone + country code in DB?
Currently thinking to seperate country_code and phone_number
$table->string('phone_country_code')->nullable();
$table->string('phone_number')->nullable();
However, brings me to questions such as:
->wherePhone($phoneNo)
How are you guys storing?
'xxxxx => 'required|phone:AUTO,US,AE'
now i use validation rule like this and it works fine with +(country code)(mobile no)
but requirement is instead of +9 start with 00
how i can do this
Basically, my validation rule is defined as $rules = ['mobile_number'=>'required|phone:IN,mobile']
when the input is
The mobile number field contains an invalid number.
NumberParseException in PhoneNumberUtil.php line 1477: The string supplied is too short to be a phone number.
NumberParseException in PhoneNumberUtil.php line 1381: The phone number supplied was null.
I am expecting the Exception messages to be returned as validation error messages.
Hope reporting the issue help improving this lovely package.
Using "propaganistas/laravel-phone": "~1.2" in composer.json led to errors. I wonder if that is because it is not publicly marked as a version at
https://packagist.org/packages/propaganistas/laravel-phone
Where the latest stable listed is version 1.1
Using "propaganistas/laravel-phone": "dev-master"
fixes the problem.
This issue does not happen with 2.5.0
NumberParseException in PhoneNumberUtil.php line 1378:
Could not interpret numbers after plus-sign.
in PhoneNumberUtil.php line 1378
at PhoneNumberUtil->parseHelper('+04(6)7912844048', 'US', false, true, object(PhoneNumber)) in PhoneNumberUtil.php line 2673
at PhoneNumberUtil->parse('+04(6)7912844048', 'US') in helpers.php line 18
at phone_format('+04(6)7912844048', 'US') in 5678c1f644f72cac8fb1af3360688d937637a2f8.php line 29
at include('C:\laragon\www\inventory\storage\framework\views\5678c1f644f72cac8fb1af3360688d937637a2f8.php') in PhpEngine.php line 42
at PhpEngine->evaluatePath('C:\laragon\www\inventory\storage\framework\views/5678c1f644f72cac8fb1af3360688d937637a2f8.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'people' => object(Collection))) in CompilerEngine.php line 59
at CompilerEngine->get('C:\laragon\www\inventory\resources\views/person/index.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'people' => object(Collection))) in View.php line 135
at View->getContents() in View.php line 106
at View->renderContents() in View.php line 80
at View->render() in Response.php line 52
at Response->setContent(object(View)) in Response.php line 197
at Response->__construct(object(View)) in Router.php line 1030
at Router->prepareResponse(object(Request), object(View)) in ControllerDispatcher.php line 95
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(PersonController), object(Route), object(Request), 'index') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\PersonController', 'index') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 703
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Authenticate.php line 28
at Authenticate->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Authenticate), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 705
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 678
at Router->dispatchToRoute(object(Request)) in Router.php line 654
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
The following returns an error "InvalidParameterException" when you use a validation rule such as
'phone' => 'phone:US,BE,mobile'
It works if you remove the mobile instead:
'phone' => 'phone:US,BE'
Curiously, fixed_line works.
Laravel 5.5 validation rule of 'phone:GB' is validating as though it is a required field. If the field is empty it is still validating.
I not able to validate on custom_country_field
Rule alpha|size:2
not apply on this field
'phonefield' => 'phone:custom_country_field',
'custom_country_field' => 'required_with:phonefield|alpha|size:2',
Hello, I'd like to use the formatter like the validator with auto-discovery and fallback countries
phone($phone_number, $country_code = "AUTO,CH,...", $format = PhoneNumberFormat::INTERNATIONAL)
So, I want to store fomatted phone numbers in my database without using the "country" field. Because I already have a country field in my database and I also use it as last fall-back method. And I don't want to annoy or confuse any people by asking the country again. It would be a very nice addition.
Hello,
I was wondering. This lib is really nice for validation.
But for example I need an output format like:
+1.123456
so in general:
+COUNTRY_CODE then PERIOD then PHONE NUMBER.
Does this library allows this?
Hello, I have a problem with dev-3.0
I can't use this validation string anymore:
'phone_number' => 'required|phone:AUTO,CH,country',
this is fine:
'phone_number' => 'required|phone:AUTO,country',
this is also fine:
'phone_number' => 'required|phone:AUTO,country',
Where country is a select field with your other LaravelIntl library.
It did work with the older version.
The error:
(1/1) InvalidParameterException
Invalid phone validation parameters: "CH".
in InvalidParameterException.php (line 17)
--
at InvalidParameterException::parameters(object(Collection))in Phone.php (line 122)
at Phone->extractParameters('phone_number', array('AUTO', 'CH', 'country'), array('_token' => '***', '_method' => 'PATCH', 'title' => '***', 'old_customer_number' => null,
...more fields...
'country' => 'CH', 'phone_number' => '+41 ****')in Phone.php (line 47)
at Phone->validate('phone_number', '+41 ****', array('AUTO', 'CH', 'country'), object(Validator))
I censored the number with ****
So basically I can't use AUTO, Fallback Country AND Fallback secondary field.
(Also tested where Fallback country and Fallback field are different)
Main reason why I don't want to use a dedicated country field for phone number because I don't want to confuse customers with problaly "unnecessary" fields. Because most of our customers live in Switzerland and will most likely have a Swiss phone number anyway. But if not then I wan't to fallback to "their" country just for the sake of convenience. And if this does not pass I'll show an error, where they will be asked to add the country code to their number. I mean, I can leave this as is and only use one hardcoded country field... Or use the country as a fallback country... But this method seems the most convenient to me and it did work before... Maybe... At least it did not throw an error. :D
I hope you understand what I mean.
Am I doing something wrong here? Is it a bug? Is there a better way of doint things?
Maybe you should add a disclaimer to warn people that this package is loaded on every request, along with the packages it requires itself like Translation, causing a significant slowdown of runtime.
Many people just need this on a few pages, in that case they would probably be better off with a simple regex or with an adaptation of your package to handle autoloading more efficiently.
Anyway, thanks for sharing!
I know it is possible in the dependency but wonder if this is available in the package
Hi,
Thanks for making this library!
Maybe it's a configuration error on my side, but if I use the "fixed_line_or_mobile" number constraint, all numbers are invalid.
$this->validate($request, [
'phone_number' => 'required|phone:CH,fixed_line_or_mobile',
])
But if I use booth everything works as expected (mobile and fixed_line numbers are valid):
$this->validate($request, [
'phone_number' => 'required|phone:CH,fixed_line,mobile',
])
Did I miss something?
Hmm, it is strange, but indian flag is missing
+91
This does not validate 1112224444, sometimes
This does valadate 111-aaa-4444, sometimes
This does validate 111-222-4444, sometimes
Using Laravel 5.5.21
Hi could you update the version of the libphonenumber referenced in composer.json.
A new version 7.* is out and although I haven't tested it, I don't think it breaks backwards compatibility.
Keep up the good work 👍 .
Hello,
I'd like to validate phone numbers I already have in my database, in order to fix the wrong ones.
How can I achieve this ?
I can't figure out how to call isValidNumber
manually.
Can you help, please ?
Is it possible to configure the name of the Country field? Using phone_country
as my field name does not make any sense (and does not fit in with my database design).
Hi there, 2.4.1 doesn't appear to be working with Laravel 4.2. Is this expected (documentation seems to indicate it should)?
Running phpunit against it comes up with a ton of errors;
PHPUnit 4.2.2 by Sebastian Bergmann.
EEEEEFF.
Time: 85 ms, Memory: 4.50Mb
There were 5 errors:
1) PhoneValidatorTest::testValidatePhoneWithDefaultCountryNoType
Argument 4 passed to Propaganistas\LaravelPhone\PhoneValidator::validatePhone() must be an instance of Illuminate\Contracts\Validation\Validator, instance of Illuminate\Validation\Validator given, called in /vagrant/vendor/propaganistas/laravel-phone/tests/TestPhoneValidator.php on line 35 and defined
/vagrant/vendor/propaganistas/laravel-phone/src/PhoneValidator.php:68
/vagrant/vendor/propaganistas/laravel-phone/tests/TestPhoneValidator.php:35
/vagrant/vendor/propaganistas/laravel-phone/tests/TestPhoneValidator.php:41
etc...
Illuminate\Contracts\Validation\Validator appears to have been introduced in Laravel 5.0.
Hi, I am new to laravel. Currently using 5.4 version. After following the readme, I encounter
ErrorException in RegisterController.php line 71: Undefined index: phone
Kindly help.
Hi,
Is there some support for Lumen?
When i use this in Lumen i get this error "FatalErrorException in Facade.php line 215: Call to undefined method Laravel\Lumen\Application::getLocale()".
The files in the repo changed, phone number types are now here:
https://github.com/giggsey/libphonenumber-for-php/blob/master/src/PhoneNumberType.php
How to show validation error?
This value of the phone number field already exists.
Thanks
I'm doing form request validation and I tried using this package, but I can't seem to get it working, my validation looks like this :
<?php
namespace App\Http\Requests\Users;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
class UserCreatePost extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'email' => ['required', 'email', 'unique:users'],
'first_name' => ['required'],
'last_name' => ['required'],
'phone' => ['required', 'phone:CA', 'unique:users'],
];
}
protected function formatErrors(Validator $validator)
{
var_dump($validator->attributes());
throw new StoreResourceFailedException('Could not create new user.', $validator->errors());
}
}
I get
{
"message": "Could not create new user.",
"errors": {
"phone": [
"The phone field contains an invalid number."
]
},
"status_code": 422
}
I tried different format :
+1 123 456 7890
+1-123-456-7890
+1 (123) 456 7890
+1 (123) 456-7890
(123) 456-7890
+11234567890
Not working.
I added Propaganistas\LaravelPhone\LaravelPhoneServiceProvider::class,
in my $providers.
Thanks for helping.
It's not an issue :). This package is great.
Can you provide the same thing with postal code ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.