Skip to content

Commit 7af483b

Browse files
committed
Refactor postal code validation rule
1 parent e936725 commit 7af483b

17 files changed

+463
-822
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ All notable changes to this project will be documented in this file.
44

55
## 4.0.0
66

7+
### Fixed
8+
9+
- Type error when field under validation is not a string has been fixed
10+
711
### Changed
812

913
- Minimum Laravel version is now 10.0
1014
- Minimum PHP version is now 8.1
15+
- Using another request field as the country for validation no longer requires a separate rule
1116

1217
### Removed
1318

1419
- Lumen is no longer supported
1520
- Manual validation outside of Laravel validation has been removed
1621
- Overriding validation patterns has been removed
1722
- PostalCodes facade has been removed
23+
- Support for lowercase country codes has been removed

README.md

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6259
This package adds the following validation rules:
6360

64-
#### postal_code:foo,bar,...
61+
#### PostalCode
6562

6663
The 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

115102
The following placeholders will be automatically filled for you:
@@ -126,11 +113,6 @@ The following placeholders will be automatically filled for you:
126113

127114
Please 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

136118
This open-source software is licenced under the [MIT license](LICENSE.md).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"extra": {
5555
"laravel": {
5656
"providers": [
57-
"\\Axlon\\PostalCodeValidation\\ValidationServiceProvider"
57+
"\\Axlon\\PostalCodeValidation\\Support\\ValidationServiceProvider"
5858
]
5959
}
6060
}

src/Extensions/PostalCode.php

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/Extensions/PostalCodeFor.php

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)