Member-only story
PhonePhone Validation in Laravel Using Abstract
Our sales events platform Auctibles, collects phone numbers for several purposes:
- Contact phone of sellers to be displayed to buyers
- Landline phone of sellers
- Mobile phone of sellers for coordination of deliveries
- Mobile phone of buyers for coordination of deliveries
Phone validation in Laravel is a crucial step for our sales events platform. It plays a significant role in ensuring smooth delivery coordination and reducing the risk of fraud.
We use validation rules, a practical feature of the PHP Laravel framework, to ensure reliable validation. This is where the Abstract comes in, enhancing the functionality of our platform.
The Validation Rule
We define a validation rule, AbstractPhoneValidation
that receives two parameters:
- The type of phone number expected — mobile, landline, or an empty string when any type is expected,
- The country of the phone number — two letters ISO 3166–1 alpha-2 code
The rule uses our Abstract API key from the configuration file.
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use…