@@ -21,7 +21,6 @@ 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 )
2726- [ Contributing] ( #contributing )
@@ -47,7 +46,7 @@ package manually, you can do this by adding the following line to your `config/a
4746``` php
4847'providers' => [
4948 ...
50- Axlon\PostalCodeValidation\ValidationServiceProvider::class,
49+ Axlon\PostalCodeValidation\Support\ ValidationServiceProvider::class,
5150 ...
5251],
5352```
@@ -61,55 +60,45 @@ framework validation rule.
6160
6261This package adds the following validation rules:
6362
64- #### postal_code : foo ,bar,...
63+ #### PostalCode
6564
6665The 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.
66+ countries in [ ISO 3166-1 alpha-2] ( https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 ) format:
6867
6968``` 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.
69+ use Axlon\PostalCodeValidation\Rules\PostalCode;
7770
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'
71+ $request->validate([
72+ 'postal_code ' => [
73+ PostalCode::of(['NL', 'BE']),
74+ ] ,
75+ ]);
8376```
8477
85- ### Fluent API
86-
87- If you prefer using a fluent object style over string based rules, that's also available:
78+ It is also possible to use another field in the request as the country code, for example:
8879
8980``` php
90- 'postal_code' => [
91- PostalCode::for('NL')->or('BE'),
92- ],
81+ use Axlon\PostalCodeValidation\Rules\PostalCode;
82+
83+ $request->validate([
84+ 'shipping.country' => ['...'],
85+ 'shipping.postal_code' => [
86+ 'required_with:shipping.country',
87+ PostalCode::of('shipping.country'),
88+ ],
89+ ]);
9390```
9491
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- ```
92+ > Note that postal code validation will pass if the referenced field is empty or missing, as long as the value under
93+ > validation is a string. It is recommended that you use some additional validation rules to ensure both fields are
94+ > present when they should be.
10595
10696### Adding an error message
10797
108- To add a meaningful error message, add the following lines to ` resources/lang/{your language}/validation.php ` :
98+ To add a meaningful error message, add the following line to ` resources/lang/{your language}/validation.php ` :
10999
110100``` php
111101'postal_code' => 'Your message here',
112- 'postal_code_with' => 'Your message here',
113102```
114103
115104The following placeholders will be automatically filled for you:
0 commit comments