Skip to content
프로젝트

axlon/laravel-postal-code-validation

GitHub에서 보기

소개

Worldwide postal code validation for Laravel

웹 및 앱 개발

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

[!WARNING] This documentation is for version 4.x, which is still in development. For production, use version 3.x and follow the 3.x documentation.

Laravel Postal Code Validation

Adds postal code validation to Laravel, based on Google's libaddressinput.

Tests Downloads Latest version License

Requirements

This package has the following requirements:

  • Laravel 12 or greater
  • PHP 8.2 or greater

Installation

You can install this package with Composer, by running the command below:

composer require axlon/laravel-postal-code-validation

Laravel registers the service provider through package discovery. To register it manually, add it to your Laravel application's bootstrap/providers.php file:

return [
    // ...
    Axlon\PostalCodeValidation\PostalCodeServiceProvider::class,
];

Usage

Validate a postal code

The field under validation must be a valid postal code in at least one of the specified areas. Arguments must be countries in ISO 3166-1 alpha-2 format:

use Axlon\PostalCodeValidation\Rules\PostalCode;

request()->validate([
    'address.postal_code' => ['required', PostalCode::of(['NL', 'BE'])],
]);

Validate a postal code against a dynamic area

You may also validate a postal code against an area code that is passed in the request:

use Axlon\PostalCodeValidation\Rules\PostalCode;

request()->validate([
    'address.country' => ['required', 'in:NL,BE'],
    'address.postal_code' => ['required', PostalCode::of('address.country')],
]);

String notation

Use postal_code with one or more comma-separated parameters. Just like with the object rule, you can mix literal area codes and input field references:

request()->validate([
-    'address.postal_code' => ['required', PostalCode::of(['NL', 'BE])],
+    'address.postal_code' => 'required|postal_code:NL,BE',
]);

Adding an error message

To add a meaningful error message, add an error message to resources/lang/{your language}/validation.php:

'postal_code' => 'The :attribute must be a valid postal code for :areas.',

The following placeholders will be automatically filled for you:

Placeholder Description
:attribute The name of the field that was under validation
:areas The areas that were validated against (e.g. NL, BE)

License

This software is licenced under the MIT license.

Attribution

This software contains data derived from Google's Address Validation Metadata. The original data is provided by Google and is licensed under the CC-BY 4.0 license.

Modifications

  • Only data relevant to postal code validation is included
  • Regular expressions found in the data are adjusted to be compatible with PHP's regex engine
  • The resulting data is converted into PHP resource files

관련 프로젝트 살펴보기

불러오는 중…