respect-validation/docs/validators/PostalCode.md
Alexandre Gomes Gaigalas 16148e9593 Standardize and improve validation message templates
- Remove redundant "valid" prefix:
   Date, DateTime, DateTimeDiff, Domain, Email, Iban, Imei, Ip, Isbn, Json, LanguageCode, LeapDate, LeapYear, Luhn, MacAddress, NfeAccessKey, Nif, Nip, Pesel, Phone, Pis, PolishIdCard, PostalCode, Roman, Slug, Tld, Url, Uuid, Version.

 - Remove redundant "value" suffix
   ArrayVal, BoolVal, Countable, FloatVal, IntVal, IterableVal, NumericVal, ScalarVal, StringVal.

 - Standardize "consist only of" phrasing
   Alnum, Alpha, Cntrl, Consonant, Digit, Graph, Lowercase, Printable, Punct, Space, Spaced, Uppercase, Vowel, Xdigit.

 - Improve file accessibility messages
   Directory, Executable, File, Image, Readable, SymbolicLink, Writable.

 - Improve grammar and article usage
   CreditCard, Extension, Mimetype, Regex, Size.
2026-02-03 19:58:55 +00:00

80 lines
2.4 KiB
Markdown

<!--
SPDX-License-Identifier: MIT
SPDX-FileCopyrightText: (c) Respect Project Contributors
SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
SPDX-FileContributor: Mazen Touati <mazen_touati@hotmail.com>
-->
# PostalCode
- `PostalCode(string $countryCode)`
- `PostalCode(string $countryCode, bool $formatted)`
Validates whether the input is a valid postal code or not.
```php
v::postalCode('BR')->assert('02179000');
// Validation passes successfully
v::postalCode('BR')->assert('02179-000');
// Validation passes successfully
v::postalCode('US')->assert('02179-000');
// → "02179-000" must be a postal code for "US"
v::postalCode('US')->assert('55372');
// Validation passes successfully
v::postalCode('PL')->assert('99-300');
// Validation passes successfully
```
By default, `PostalCode` won't validate the format (puncts, spaces), unless you pass `$formatted = true`:
```php
v::postalCode('BR', true)->assert('02179000');
// → "02179000" must be a postal code for "BR"
v::postalCode('BR', true)->assert('02179-000');
// Validation passes successfully
```
Message template for this validator includes `{{countryCode}}`.
Extracted from [GeoNames](http://www.geonames.org/).
## Templates
### `PostalCode::TEMPLATE_STANDARD`
| Mode | Template |
| ---------: | :-------------------------------------------------------- |
| `default` | {{subject}} must be a postal code for {{countryCode}} |
| `inverted` | {{subject}} must not be a postal code for {{countryCode}} |
## Template placeholders
| Placeholder | Description |
| ------------- | ---------------------------------------------------------------- |
| `countryCode` | |
| `subject` | The validated input or the custom validator name (if specified). |
## Categorization
- Localization
- Strings
## Changelog
| Version | Description |
| ------: | :------------------------------------------------ |
| 3.0.0 | Templates changed |
| 2.3.0 | Add option to validate formatting |
| 2.2.4 | Cambodian postal codes now support 5 and 6 digits |
| 0.7.0 | Created |
## See Also
- [CountryCode](CountryCode.md)
- [Iban](Iban.md)