Skip to content

Commit e6557b9

Browse files
committed
Update README.md
1 parent 9e5f9f4 commit e6557b9

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,56 @@ Also, you can make <a href="https://github.com/milwad-dev/laravel-validate/pulls
158158
- [x] Ukrainian (uk)
159159
- [x] Chinese (zh_CN)
160160

161+
# Adding Custom Phone Country Validator
162+
163+
If you need to add a custom phone number validator for a specific country, follow the steps below.
164+
165+
### 1. Create Your Custom Validator Class
166+
First, you need to create a custom validator class that implements the `Milwad\LaravelValidate\Utils\CountryPhoneValidator` contract. This contract ensures that your custom validator adheres to the required structure and functionality.
167+
168+
```php
169+
namespace App\Validators;
170+
171+
use Milwad\LaravelValidate\Utils\CountryPhoneValidator;
172+
173+
class CustomPhoneValidator implements CountryPhoneValidator
174+
{
175+
/**
176+
* Validate the phone number for the custom country.
177+
*/
178+
public function validate(string $phoneNumber): bool
179+
{
180+
// Implement the phone number validation logic for your country
181+
// Example: Check if the phone number matches a specific pattern
182+
return preg_match('/^\+1234\d{10}$/', $phoneNumber);
183+
}
184+
}
185+
```
186+
187+
### 2. Add the Validator to the Configuration File
188+
Once you've created the custom validator class, add it to the configuration file (`config/phone-country.php`) under the `'phone-country'` array.
189+
190+
For example, if you're adding a validator for the country `XY`:
191+
192+
```php
193+
'phone-country' => [
194+
// Existing validators...
195+
'XY' => \App\Validators\CustomPhoneValidator::class, // Custom country
196+
],
197+
```
198+
199+
This tells the system to use your custom validator for phone numbers from country `XY`.
200+
201+
### 3. Validation Usage
202+
203+
Once your custom validator is set up, you can use it in your application like any other validator:
204+
205+
```php
206+
return [
207+
'phone_ir' => [new ValidPhoneNumber('XY')],
208+
];
209+
```
210+
161211
# License
162212

163213
* This package is created and modified by <a href="https://github.com/milwad-dev" target="_blank">Milwad Khosravi</a> for Laravel over more than 9 and has been released under the MIT License.

0 commit comments

Comments
 (0)