Skip to content

Commit bd45bf5

Browse files
committed
Refactor postal code validation rule
1 parent fab5190 commit bd45bf5

19 files changed

+398
-828
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
bin/ export-ignore
44
.github/ export-ignore
5+
phpstan-stubs/ export-ignore
56
tests/ export-ignore
67
.editorconfig export-ignore
78
.gitattributes export-ignore

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 & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6261
This package adds the following validation rules:
6362

64-
#### postal_code:foo,bar,...
63+
#### PostalCode
6564

6665
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.
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

115104
The following placeholders will be automatically filled for you:

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"phpstan/extension-installer": "^1.4",
2828
"phpstan/phpstan": "^2.1",
2929
"phpstan/phpstan-deprecation-rules": "^2.0",
30+
"phpstan/phpstan-mockery": "^2.0",
3031
"phpstan/phpstan-strict-rules": "^2.0",
3132
"phpunit/phpunit": "^10.5 || ^11.5",
3233
"slevomat/coding-standard": "^8.16",
@@ -54,7 +55,7 @@
5455
"extra": {
5556
"laravel": {
5657
"providers": [
57-
"\\Axlon\\PostalCodeValidation\\ValidationServiceProvider"
58+
"\\Axlon\\PostalCodeValidation\\Support\\ValidationServiceProvider"
5859
]
5960
}
6061
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Testing\Concerns;
4+
5+
use Closure;
6+
7+
trait InteractsWithContainer
8+
{
9+
/**
10+
* @template T of object
11+
* @param class-string<T>|string $abstract
12+
* @return ($abstract is class-string<T> ? \Mockery\MockInterface&T : \Mockery\MockInterface)
13+
*/
14+
protected function mock($abstract, Closure $mock = null);
15+
}

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ parameters:
88
paths:
99
- src
1010
- tests
11+
stubFiles:
12+
- phpstan-stubs/laravel/framework/InteractsWithContainer.stub

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)