milwad-dev / laravel-validate

The Laravel-Validate package enhanced Laravel validation capabilities with custom rules and methods for simplified and efficient validation logic.
https://packagist.org/packages/milwad/laravel-validate
MIT License
456 stars 39 forks source link

[1.x] Validate phone by country code #49

Closed Claudye closed 1 year ago

Claudye commented 1 year ago

Users will now have the option to specify an optional parameter when instantiating the ValidPhoneNumber class, which will allow them to validate phone numbers for a specific country. If no parameter is specified, the default country will be used for validation.

Claudye commented 1 year ago

I wrote a class

Milwad\LaravelValidate\Utils\CountryPhoneCallback::class

that contains a list of methods to validate phone numbers according to the convention. Each method is named validateCODE() and returns a boolean. This class allows writing any valid PHP code (not just a regex) to validate phone numbers.

In addition to that, the class contains a method called callPhoneValidator() which allows to call the appropriate method to validate a phone number according to the provided country code.

This class takes as parameters in it constructor the phone number or a comma separated list of phone numbers, the ISO country code, and the attribute. It is called in the ValidPhoneNumber class if a $code parameter has been passed to it, otherwise it executes the regular expression that was there

Ex:

public  function  phone_number_is_valid() {
    $rules  = [
    'phone_number' => [new  ValidPhoneNumber()],
    'phone_bj' => [new  ValidPhoneNumber(Country::BENIN)]
    ];
    $data  = ['phone_number' => '09366000000', 'phone_bj' => "+22997000000"];
    $passes  =  $this->app['validator']->make($data, $rules)->passes();
    $this->assertTrue($passes);
}
milwad-dev commented 1 year ago

For example, how to validate Iran phone number? where is it!

Claudye commented 1 year ago

I don't know the phone number format of all countries. I did first if accepted I will add for other countries

milwad-dev commented 1 year ago

Is important to add it soon!

Claudye commented 1 year ago

I work on already