php
40 lines · 6 steps
How a custom phone validation rule works in Laravel
A Laravel ValidationRule wraps libphonenumber to parse and validate phone numbers against a default region.
Explained by
highlit
1<?php
2
3namespace App\Rules;
4
5use Closure;
6use Illuminate\Contracts\Validation\ValidationRule;
7use libphonenumber\NumberParseException;
8use libphonenumber\PhoneNumberUtil;
9
10class ValidPhoneNumber implements ValidationRule
11{
12 public function __construct(
13 private readonly ?string $defaultRegion = 'US',
14 ) {
15 }
16
17 public function validate(string $attribute, mixed $value, Closure $fail): void
18 {
19 if (! is_string($value) || trim($value) === '') {
20 $fail('The :attribute must be a valid phone number.')->translate();
21
22 return;
23 }
24
25 $util = PhoneNumberUtil::getInstance();
26
27 try {
28 $number = $util->parse($value, $this->defaultRegion);
29 } catch (NumberParseException) {
30 $fail('The :attribute is not a recognizable phone number.')->translate();
31
32 return;
33 }
34
35 if (! $util->isValidNumber($number)) {
36 $fail('The :attribute is not a valid phone number for the :region region.')
37 ->translate(['region' => $this->defaultRegion]);
38 }
39 }
40}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Implementing ValidationRule lets you encapsulate complex domain checks as a reusable, injectable object.
- 2Calling $fail and returning early keeps each failure path isolated and prevents downstream code from running on bad input.
- 3Delegating phone parsing to libphonenumber gives you region-aware correctness instead of fragile regex guesses.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-custom-phone-validation-rule-works-in-laravel-explained-php-a54c/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.