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
typescript
import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { ValidationPipe } from '@nestjs/common'; import { ApiProperty } from '@nestjs/swagger';
Wiring validation and Swagger docs in NestJS
validation
openapi
decorators
Intermediate
8 steps
php
<?php namespace App\Console\Commands;
Releasing stale document locks in Laravel
artisan-command
transactions
row-locking
Intermediate
6 steps
ruby
class TemplateInterpolator PLACEHOLDER = /\{\{\s*([\w.]+)\s*\}\}/ def initialize(strict: false)
Interpolating templates with dotted keys in Ruby
regex
string-interpolation
hash-traversal
Intermediate
6 steps
php
<?php namespace App\Providers;
Subdomain multi-tenancy routing in Laravel
multi-tenancy
service-container
route-binding
Advanced
7 steps
php
<?php namespace App\Broadcasting;
Authorizing presence channels in Laravel
broadcasting
authorization
presence-channels
Intermediate
3 steps
go
package config import ( "fmt"
A thread-safe config singleton in Go
singleton
concurrency
environment-variables
Intermediate
7 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.