@@ -21,11 +21,8 @@ Worldwide postal code validation for Laravel, based on Google's Address Data Ser
2121- [ Installation] ( #installation )
2222- [ Usage] ( #usage )
2323 - [ Available rules] ( #available-rules )
24- - [ Fluent API] ( #fluent-api )
2524 - [ Adding an error message] ( #adding-an-error-message )
2625- [ Changelog] ( #changelog )
27- - [ Contributing] ( #contributing )
28- - [ Credits] ( #credits )
2926- [ License] ( #license )
3027
3128## Requirements
@@ -47,7 +44,7 @@ package manually, you can do this by adding the following line to your `config/a
4744``` php
4845'providers' => [
4946 ...
50- Axlon\PostalCodeValidation\ValidationServiceProvider::class,
47+ Axlon\PostalCodeValidation\Support\ ValidationServiceProvider::class,
5148 ...
5249],
5350```
@@ -61,55 +58,45 @@ framework validation rule.
6158
6259This package adds the following validation rules:
6360
64- #### postal_code : foo ,bar,...
61+ #### PostalCode
6562
6663The field under validation must be a valid postal code in at least one of the given countries. Arguments must be
67- countries in [ ISO 3166-1 alpha-2] ( https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 ) format.
64+ countries in [ ISO 3166-1 alpha-2] ( https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 ) format:
6865
6966``` php
70- 'postal_code' => 'postal_code:NL,DE,FR,BE'
71- ```
72-
73- #### postal_code_with: foo ,bar,...
74-
75- The field under validation must be a postal code in at least one of the countries in the given fields _ only if_ at least
76- one of the specified fields is present.
67+ use Axlon\PostalCodeValidation\Rules\PostalCode;
7768
78- ``` php
79- 'billing.country ' => 'required|string|max:2',
80- ...
81- 'shipping.country' => 'nullable|string|max:2' ,
82- 'shipping.postal_code' => 'postal_code_with:billing.country,shipping.country'
69+ $request->validate([
70+ 'postal_code ' => [
71+ PostalCode::of(['NL', 'BE']),
72+ ] ,
73+ ]);
8374```
8475
85- ### Fluent API
86-
87- If you prefer using a fluent object style over string based rules, that's also available:
76+ It is also possible to use another field in the request as the country code, for example:
8877
8978``` php
90- 'postal_code' => [
91- PostalCode::for('NL')->or('BE'),
92- ],
79+ use Axlon\PostalCodeValidation\Rules\PostalCode;
80+
81+ $request->validate([
82+ 'shipping.country' => ['...'],
83+ 'shipping.postal_code' => [
84+ 'required_with:shipping.country',
85+ PostalCode::of('shipping.country'),
86+ ],
87+ ]);
9388```
9489
95- The same goes for the ` postal_code_with ` rule:
96-
97- ``` php
98- 'billing.country' => 'required|string|max:2',
99- ...
100- 'shipping.country' => 'nullable|string|max:2',
101- 'shipping.postal_code' => [
102- PostalCode::with('billing.country')->or('shipping.country')
103- ],
104- ```
90+ > Note that postal code validation will pass if the referenced field is missing as long as the value under validation is
91+ > a string. It is recommended that you use additional validation rules to ensure both fields are present when they
92+ > should be.
10593
10694### Adding an error message
10795
108- To add a meaningful error message, add the following lines to ` resources/lang/{your language}/validation.php ` :
96+ To add a meaningful error message, add the following line to ` resources/lang/{your language}/validation.php ` :
10997
11098``` php
11199'postal_code' => 'Your message here',
112- 'postal_code_with' => 'Your message here',
113100```
114101
115102The following placeholders will be automatically filled for you:
@@ -126,11 +113,6 @@ The following placeholders will be automatically filled for you:
126113
127114Please see [ CHANGELOG] ( CHANGELOG.md ) for more information on what has changed recently.
128115
129- ## Credits
130-
131- - [ Choraimy Kroonstuiver] ( https://github.com/axlon )
132- - [ All contributors] ( https://github.com/axlon/laravel-postal-code-validation/contributors )
133-
134116## License
135117
136118This open-source software is licenced under the [ MIT license] ( LICENSE.md ) .
0 commit comments