Skip to content
Devix Open Source

Guide

Validation

The rule object

use Devix\Phone\Laravel\Rules\Phone;

Phone::rule()                              // any valid number with a calling code (or the default country)
    ->country('AE', 'SA')                  // only these countries; national numbers are tried as each
    ->defaultCountry('AE')                 // read national numbers as AE without restricting countries
    ->countryField('billing_country')      // read the country from another input
    ->mobile()                             // or ->fixed(), ->type('mobile', 'voip')
    ->notType('premium', 'toll_free')
    ->international()                      // require +… or 00…
    ->lenient();                           // accept numbers of a possible length, even if unassigned

Rule::phone() returns the same object.

The string rule

Parameters can come in any order:

Parameter Meaning
AE, PK, … Allowed countries (ISO 3166-1 alpha-2, upper case)
mobile, fixed, toll_free, … Allowed types
!premium Rejected type
international Require a calling code
lenient Possible length is enough
anything else The name of the input holding the country
'phone' => 'required|phone:AE,SA,mobile',
'office' => 'nullable|phone:office_country,fixed',

Where the country comes from

A number with + or 00 carries its own country. For national numbers like 050 123 4567 the rule tries, in order:

  1. The country field — countryField(), or <field>_country when the request has one. The <x-phone-input> component posts exactly that.
  2. defaultCountry().
  3. Each country passed to country().
  4. config('phone.default_country').

The first reading that passes wins. If none does, the error comes from the most promising one, so someone who typed a valid number from the wrong country is told must be a number from…, not invalid.

Messages

Each failure has its own message:

Reason English message
too_short The :attribute is too short.
too_long The :attribute is too long.
invalid_length The :attribute has the wrong number of digits.
invalid_country_code The :attribute needs a valid country code.
invalid_number The :attribute is not a valid phone number.
possible_local_only The :attribute needs its area code.
not_allowed_type The :attribute must be a :types number.
not_allowed_country The :attribute must be a number from :countries.
not_international The :attribute must include the country code, like :example.

:types, :countries and :example are filled in for you — the example is a real number from the right country in the right format.

English, Arabic and Urdu are included. Publish them to edit the wording or add a language:

php artisan vendor:publish --tag=phone-lang

A message you define for the field still takes priority:

$request->validate(['phone' => 'phone:AE'], ['phone.phone' => 'Please use a UAE number.']);

Reading the validated number

$phone = $request->phone('phone');   // PhoneNumber, using phone_country when posted
$user->update(['phone' => $phone]);  // the cast stores E.164

Why browser and server always agree

The widget and this package read the same compiled metadata and share their algorithms line for line. Every release runs more than 17,000 inputs through both — every example number for every country, typed nationally, internationally, with 00, in Arabic-Indic and Persian digits, formatted, cut short, too long, with extensions — and fails if a single answer differs.

Updated 12 Sep 2026