chisnall/postcode
最新稳定版本:v1.0.1
Composer 安装命令:
composer require chisnall/postcode
包简介
Postcode validator for Laravel
README 文档
README
A postcode validator for Laravel.
Handles UK postcodes including the Isle of Man and the Channel Islands.
Designed to be used with Laravel's built-in form validation but also supports manual usage.
It works with Laravel versions 10 to 12.
Features
- Checks postcode against an external API
- Uses ONS and Ordnance Survey postcode data
- Determines postcode country
- Supports England, Wales, Scotland and Northern Ireland
- Supports the Isle of Man and the Channel Islands
- Falls back to regex validation if the external API is unavailable
- Allows countries or areas to be excluded
Installation
Install with composer:
composer require chisnall/postcode
Status Codes
| Status Code | Form validation | Description |
|---|---|---|
| 200 | N/A | The postcode is valid. |
| 403 | postcode.excluded |
The postcode is excluded by country or area. |
| 404 | postcode.not_found |
The postcode was not found. |
| 422 | postcode.not_valid |
The postcode format is not valid. |
| 503 | postcode.service_error |
The external API is not valid. |
Usage - Form Validation
The form validation messages can be customised from the status code returned.
There is a catch-all message: input.postcode
However, it is recommended to use the status code specific messages.
This provides a better user experience.
Use this in the controller which handles the form submission:
$validated = $request->validate([
'input' => 'required|postcode',
],
[
'input.required' => 'Please enter your postcode.',
'input.postcode' => 'This will catch all errors if specific status codes are not handled.',
'input.postcode.excluded' => 'The postcode or area is excluded.',
'input.postcode.not_found' => 'The postcode is not found.',
'input.postcode.not_valid' => 'The postcode format is not valid.',
'input.postcode.service_error' => 'The external API is unavailable and regex fallback is disabled.',
]);
The priority in which the form messages are shown is:
- Specific status code message (if defined)
- The catch-all message (if defined)
- Default system message
Configuration
The default configuration can be changed using ENV variables.
You may not be legally able to provide your services to certain countries.
Or you ship physical goods and the shipping cost is prohibitive to certain areas.
In these cases you can exclude countries or areas.
For form validation, this is done in the global configuration though ENV variables.
| ENV Variable | Default | Description |
|---|---|---|
POSTCODE_API_ENDPOINT |
https://api.postcodes.io/postcodes/ | The URL of the external API. |
POSTCODE_REGEX_FORCE |
false | Force regex. Disables the external API. |
POSTCODE_REGEX_FALLBACK |
true | Fallback to regex if external API is down. |
POSTCODE_EXCLUDE_COUNTRIES |
null | Comma separated list of countries to exclude. |
POSTCODE_EXCLUDE_AREAS |
null | Comma separated list of areas to exclude. |
Notes
- If you want to test the 503 service error, set the API endpoint so it is unreachable. For example, a path in localhost.
- The list of countries to exclude is case-insensitive.
- The list of areas to exclude is also case-insensitive.
- The area is the first part of the postcode. For example,
SW1A 1AAis in the areaSW.
Usage - Manual
Validation can be done manually as a facade pattern.
Each facade call generates a new instance rather than reusing a cached one.
Global configuration from the ENV variables still applies when using the facade, but can be overridden using the facade methods.
Basic usage:
$postcode = Postcode::lookup('SW1A 1AA');
That will return an instance of the postcode object.
These methods are available and can be chained:
| Method | Input | Description |
|---|---|---|
excludeAreas |
string or array | Add an area to the exclude list. |
excludeCountries |
string or array | Add a country to the exclude list. |
forceRegex |
null, true or false | Force regex. Defaults to true if no input. |
includeAreas |
string or array | Force include an area already excluded. |
includeCountries |
string or array | Force include a country already excluded. |
lookup |
string | Perform the postcode lookup. Do this after the other methods. |
toArray |
N/A | Convert result to array. Use at the end of the chain. |
For example:
$postcode = Postcode::forceRegex()->lookup('SW1A 1AA')->getStatus();
These getters are available:
| Method | Returns | Description |
|---|---|---|
getPostcode |
string | The postcode used for the lookup. |
getArea |
string | The area used for the lookup. |
getApiEndpoint |
string | The API endpoint used. |
getApiCall |
bool | If the API was used. |
getApiError |
null or string | The API error. |
getRegexUsed |
bool | If regex was used. |
getRegexForced |
bool | If regex was forced. |
getExcludeAreas |
array | Array of excluded areas. |
getExcludeCountries |
array | Array of excluded countries. |
getStatus |
int | The lookup status code. |
getResult |
array | Array of the lookup result. |
getError |
null or string | The lookup error. |
The getResult method returns an array with the following keys:
| Key | Type | Description |
|---|---|---|
postcode |
string | The postcode in the correct format. |
admin_district |
string | The admin (council) area. |
country |
string | The country. |
The admin district is "unknown" if the regex service is used.
Regex Fallback
If the external API is unavailable, the regex fallback will be used, unless disabled.
The regex can also be forced, which will disable the external API.
Caveats regarding the regex service:
- Unable to determine if a postcode actually exists. Only if the format is correct.
- Unable to determine the admin district. Always returns "unknown".
- Cannot reliably determine a country. This uses the area part of the postcode and some postcode areas overlap countries.
For example:
CH (Chester) - overlaps England and Wales.
SY (Shrewsbury) - overlaps England and Wales.
TD (Galashiels) - overlaps Scotland and England.
This is where the external API should be used to reliably determine if the postcode actually exists and to get the correct country.
Publish
The configuration can be published to allow further customisation.
artisan vendor:publish --tag=postcode-config
The translations can be published to allow further customisation.
artisan vendor:publish --tag=lang
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-15
