Code Monkey home page Code Monkey logo

laravel-phone's Introduction

Laravel Phone Validator

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads License

Adds a phone validator to Laravel 4|5 and Lumen based on the PHP port of Google's libphonenumber API by giggsey.

Installation

Run the following command to install the latest version of the package

composer require propaganistas/laravel-phone

Laravel

In your app config, add the Service Provider to the $providers array

'providers' => [
   ...
   Propaganistas\LaravelPhone\LaravelPhoneServiceProvider::class,
],

In your languages directory, add for each language an extra language line for the validator:

"phone" => "The :attribute field contains an invalid number.",

Lumen

In bootstrap/app.php, register the Service Provider

$app->register(Propaganistas\LaravelPhone\LaravelPhoneServiceProvider::class);

Usage

To validate a field using the phone validator, use the phone keyword in your validation rules array. The phone validator is able to operate in three ways.

'phonefield' => 'phone:US,BE', ```

The validator will check if the number is valid in at least one of provided countries, so feel free to add as many country codes as you like.

  • You don't specify any parameters but you plug in a dedicated country input field (keyed by ISO 3166-1 compliant country codes) to allow end users to supply a country on their own. The easiest method by far is to install the CountryList package by monarobase. Make sure the country field is named similar as the phone field but with _country appended for automatic discovery, or provide your custom country field name as a parameter to the validator:

'phonefield' => 'phone', 'phonefield_country' => 'required_with:phonefield', ```

```php

'phonefield' => 'phone:custom_country_field', 'custom_country_field' => 'required_with:phonefield', ```

If using the CountryList package, you could then use the following snippet to populate a country selection list:

```php

Countries::getList(App::getLocale(), 'php', 'cldr')) ```

  • You instruct the validator to detect which country the number belongs to using the AUTO keyword (and optionally any fallback countries):

'phonefield' => 'phone:AUTO,US', ```

The validator will try to extract the country from the number itself and then check if the number is valid for that country. If the country could not be guessed it will be validated using the fallback countries if provided. Note that country guessing will only work when phone numbers are entered in international format (prefixed with a + sign, e.g. +32 ....). Leading double zeros will NOT be parsed correctly as this isn't an established consistency.

To specify constraints on the number type, just append the allowed types to the end of the parameters, e.g.:

'phonefield'  => 'phone:US,BE,mobile',

The most common types are mobile and fixed_line, but feel free to use any of the types defined here.

You can also enable more lenient validation (for example, fixed lines without area codes) by using the LENIENT parameter. This feature inherently doesn't play well with country autodetection and number type validation, so use such combo at own risk.

'phonefield'  => 'phone:LENIENT,US',

Display

Format a fetched phone value using the helper function. $country_code is the country the phone number belongs to.

phone_format($phone_number, $country_code = null, $format = PhoneNumberFormat::INTERNATIONAL)

If no $country_code was given, the current application locale will be used as default. The $format parameter is optional and should be a constant of libphonenumber\PhoneNumberFormat (defaults to libphonenumber\PhoneNumberFormat::INTERNATIONAL)

laravel-phone's People

Contributors

giggsey avatar gounlaf avatar mikemand avatar propaganistas avatar vinkla avatar xfxf avatar

Watchers

 avatar  avatar  avatar

Forkers

glhd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.