Skip to content

Commit 8d8d809

Browse files
authored
Merge pull request #103 from milwad-dev/add-valid-country-rule
[1.x] Add `ValidCountry` - +40 Rules
2 parents 65701df + 7a28e67 commit 8d8d809

29 files changed

+339
-33
lines changed

README.md

Lines changed: 34 additions & 33 deletions
Large diffs are not rendered by default.

config/laravel-validate.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,167 @@
4949
* If you want to use rules like 'required|ValidPhone' in your validations, you can change it to true.
5050
*/
5151
'using_container' => false,
52+
53+
/*
54+
* The list of country names.
55+
*
56+
* If any country name is missing from this list, you can add your country name to this array.
57+
*/
58+
'countries' => [
59+
'AF' => 'Afghanistan',
60+
'AL' => 'Albania',
61+
'DZ' => 'Algeria',
62+
'AS' => 'American Samoa',
63+
'AD' => 'Andorra',
64+
'AO' => 'Angola',
65+
'AG' => 'Antigua and Barbuda',
66+
'AR' => 'Argentina',
67+
'AM' => 'Armenia',
68+
'AU' => 'Australia',
69+
'AT' => 'Austria',
70+
'AZ' => 'Azerbaijan',
71+
'BS' => 'Bahamas',
72+
'BH' => 'Bahrain',
73+
'BD' => 'Bangladesh',
74+
'BB' => 'Barbados',
75+
'BY' => 'Belarus',
76+
'BE' => 'Belgium',
77+
'BZ' => 'Belize',
78+
'BJ' => 'Benin',
79+
'BT' => 'Bhutan',
80+
'BO' => 'Bolivia',
81+
'BA' => 'Bosnia and Herzegovina',
82+
'BW' => 'Botswana',
83+
'BR' => 'Brazil',
84+
'BN' => 'Brunei',
85+
'BG' => 'Bulgaria',
86+
'BF' => 'Burkina Faso',
87+
'BI' => 'Burundi',
88+
'KH' => 'Cambodia',
89+
'CM' => 'Cameroon',
90+
'CA' => 'Canada',
91+
'CV' => 'Cape Verde',
92+
'CF' => 'Central African Republic',
93+
'TD' => 'Chad',
94+
'CL' => 'Chile',
95+
'CN' => 'China',
96+
'CO' => 'Colombia',
97+
'KM' => 'Comoros',
98+
'CG' => 'Congo',
99+
'CR' => 'Costa Rica',
100+
'HR' => 'Croatia',
101+
'CU' => 'Cuba',
102+
'CY' => 'Cyprus',
103+
'CZ' => 'Czech Republic',
104+
'DK' => 'Denmark',
105+
'DJ' => 'Djibouti',
106+
'DM' => 'Dominica',
107+
'DO' => 'Dominican Republic',
108+
'EC' => 'Ecuador',
109+
'EG' => 'Egypt',
110+
'SV' => 'El Salvador',
111+
'GQ' => 'Equatorial Guinea',
112+
'ER' => 'Eritrea',
113+
'EE' => 'Estonia',
114+
'ET' => 'Ethiopia',
115+
'FJ' => 'Fiji',
116+
'FI' => 'Finland',
117+
'FR' => 'France',
118+
'GA' => 'Gabon',
119+
'GM' => 'Gambia',
120+
'GE' => 'Georgia',
121+
'DE' => 'Germany',
122+
'GH' => 'Ghana',
123+
'GR' => 'Greece',
124+
'GD' => 'Grenada',
125+
'GT' => 'Guatemala',
126+
'GN' => 'Guinea',
127+
'GY' => 'Guyana',
128+
'HT' => 'Haiti',
129+
'HN' => 'Honduras',
130+
'HU' => 'Hungary',
131+
'IS' => 'Iceland',
132+
'IN' => 'India',
133+
'ID' => 'Indonesia',
134+
'IR' => 'Iran',
135+
'IQ' => 'Iraq',
136+
'IE' => 'Ireland',
137+
'IL' => 'Israel',
138+
'IT' => 'Italy',
139+
'JM' => 'Jamaica',
140+
'JP' => 'Japan',
141+
'JO' => 'Jordan',
142+
'KZ' => 'Kazakhstan',
143+
'KE' => 'Kenya',
144+
'KR' => 'South Korea',
145+
'KW' => 'Kuwait',
146+
'KG' => 'Kyrgyzstan',
147+
'LA' => 'Laos',
148+
'LV' => 'Latvia',
149+
'LB' => 'Lebanon',
150+
'LY' => 'Libya',
151+
'LT' => 'Lithuania',
152+
'LU' => 'Luxembourg',
153+
'MG' => 'Madagascar',
154+
'MY' => 'Malaysia',
155+
'MV' => 'Maldives',
156+
'ML' => 'Mali',
157+
'MT' => 'Malta',
158+
'MX' => 'Mexico',
159+
'MD' => 'Moldova',
160+
'MC' => 'Monaco',
161+
'MN' => 'Mongolia',
162+
'ME' => 'Montenegro',
163+
'MA' => 'Morocco',
164+
'MZ' => 'Mozambique',
165+
'MM' => 'Myanmar',
166+
'NA' => 'Namibia',
167+
'NP' => 'Nepal',
168+
'NL' => 'Netherlands',
169+
'NZ' => 'New Zealand',
170+
'NI' => 'Nicaragua',
171+
'NE' => 'Niger',
172+
'NG' => 'Nigeria',
173+
'NO' => 'Norway',
174+
'OM' => 'Oman',
175+
'PK' => 'Pakistan',
176+
'PA' => 'Panama',
177+
'PY' => 'Paraguay',
178+
'PE' => 'Peru',
179+
'PH' => 'Philippines',
180+
'PL' => 'Poland',
181+
'PT' => 'Portugal',
182+
'QA' => 'Qatar',
183+
'RO' => 'Romania',
184+
'RU' => 'Russia',
185+
'SA' => 'Saudi Arabia',
186+
'SN' => 'Senegal',
187+
'RS' => 'Serbia',
188+
'SG' => 'Singapore',
189+
'SK' => 'Slovakia',
190+
'SI' => 'Slovenia',
191+
'ZA' => 'South Africa',
192+
'ES' => 'Spain',
193+
'LK' => 'Sri Lanka',
194+
'SE' => 'Sweden',
195+
'CH' => 'Switzerland',
196+
'SY' => 'Syria',
197+
'TW' => 'Taiwan',
198+
'TJ' => 'Tajikistan',
199+
'TH' => 'Thailand',
200+
'TN' => 'Tunisia',
201+
'TR' => 'Turkey',
202+
'TM' => 'Turkmenistan',
203+
'UA' => 'Ukraine',
204+
'AE' => 'United Arab Emirates',
205+
'GB' => 'United Kingdom',
206+
'US' => 'United States',
207+
'UY' => 'Uruguay',
208+
'UZ' => 'Uzbekistan',
209+
'VE' => 'Venezuela',
210+
'VN' => 'Vietnam',
211+
'YE' => 'Yemen',
212+
'ZM' => 'Zambia',
213+
'ZW' => 'Zimbabwe',
214+
],
52215
];

docs/1.x/valid-country.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## ValidCountry
2+
3+
If you want to validate the country name, you can use the `ValidCountry` rule:
4+
5+
```php
6+
use Milwad\LaravelValidate\Rules\ValidCountry;
7+
8+
return [
9+
'country' => ['required', new ValidCountry()], // country => United Arab Emirates
10+
];
11+
```
12+
13+
Also, you can validate country name by key like "AE":
14+
15+
```php
16+
use Milwad\LaravelValidate\Rules\ValidCountry;
17+
18+
return [
19+
'country' => ['required', new ValidCountry(validByKey: true)], // country => "AE"
20+
];
21+
```
22+
23+
For see the list of country name, you can open `config/laravel-validate.php`, and go to `countries` key:
24+
25+
```php
26+
/*
27+
* The list of country names.
28+
*
29+
* If any country name is missing from this list, you can add your country name to this array.
30+
*/
31+
'countries' => [...],
32+
```
33+
34+
> If any country name is missing from this list, you can add your country name to this array.

src/Rules/ValidCountry.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Milwad\LaravelValidate\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
class ValidCountry implements Rule
8+
{
9+
public function __construct(
10+
protected bool $validByKey = false,
11+
) {}
12+
13+
/**
14+
* Check country name is valid.
15+
*/
16+
public function passes($attribute, $value): bool
17+
{
18+
$countries = config('laravel-validate.countries', []);
19+
20+
// Check value exists in country keys
21+
if ($this->validByKey) {
22+
return array_key_exists($value, $countries);
23+
}
24+
25+
return in_array($value, $countries);
26+
}
27+
28+
/**
29+
* Get the validation error message.
30+
*/
31+
public function message(): string
32+
{
33+
return __('validate.country');
34+
}
35+
}

src/lang/It/validate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'capital-char-with-number' => ':attribute non è valido.',
88
'car-number' => ':attribute non è valido.',
99
'cart-number-iran' => ':attribute non è valido.',
10+
'country' => ':attribute non è valido.',
1011
'credit-card' => ':attribute non è valido.',
1112
'discord-username' => ':attribute non è valido.',
1213
'domain' => ':attribute non è valido.',

src/lang/ar/validate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'capital-char-with-number' => ':attribute غير صالح.',
88
'car-number' => ':attribute غير صالح.',
99
'cart-number-iran' => ':attribute غير صالح.',
10+
'country' => ':attribute غير صالح.',
1011
'credit-card' => ':attribute غير صالح.',
1112
'discord-username' => ':attribute غير صالح.',
1213
'domain' => ':attribute غير صالح.',

src/lang/az/validate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'capital-char-with-number' => ':attribute doğru deyil.',
88
'car-number' => ':attribute doğru deyil.',
99
'cart-number-iran' => ':attribute doğru deyil.',
10+
'country' => ':attribute doğru deyil.',
1011
'credit-card' => ':attribute doğru deyil.',
1112
'discord-username' => ':attribute doğru deyil.',
1213
'domain' => ':attribute doğru deyil.',

src/lang/bn/validate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'capital-char-with-number' => ':attribute বৈধ নয়.',
88
'car-number' => ':attribute বৈধ নয়.',
99
'cart-number-iran' => ':attribute বৈধ নয়.',
10+
'country' => ':attribute বৈধ নয়.',
1011
'credit-card' => ':attribute বৈধ নয়.',
1112
'discord-username' => ':attribute বৈধ নয়.',
1213
'domain' => ':attribute বৈধ নয়.',

src/lang/ca/validate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'capital-char-with-number' => ':attribute no és vàlid.',
88
'car-number' => ':attribute no és vàlid.',
99
'cart-number-iran' => ':attribute no és vàlid.',
10+
'country' => ':attribute no és vàlid.',
1011
'credit-card' => ':attribute no és vàlid.',
1112
'discord-username' => ':attribute no és vàlid.',
1213
'domain' => ':attribute no és vàlid.',

src/lang/de/validate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'capital-char-with-number' => ':attribute kein gültiger.',
88
'car-number' => ':attribute kein gültiger.',
99
'cart-number-iran' => ':attribute kein gültiger.',
10+
'country' => ':attribute kein gültiger.',
1011
'credit-card' => ':attribute kein gültiger.',
1112
'discord-username' => ':attribute kein gültiger.',
1213
'domain' => ':attribute kein gültiger.',

0 commit comments

Comments
 (0)