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
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
php
<?php namespace App\Support;
Locale-aware formatting with PHP's intl extension
internationalization
encapsulation
constructor-injection
Intermediate
7 steps
php
<?php namespace App\Support;
Merging query params onto a URL in PHP
url-parsing
query-strings
immutability
Intermediate
8 steps
php
<?php class ImageUploadService {
Validating file uploads safely in PHP
file-upload
input-validation
security
Intermediate
8 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.