Oihana PHP System

PostalCodeRule extends Rule

Validates a postal code for a given country.

Tags
see
PostalCode
example
use Somnambulist\Components\Validation\Factory;
use xyz\enums\PostalCode ;
use xyz\validators\rules\PostalCode ;

$validator = new Factory();

$validator->addRule('postalCode', new PostalCode() );
// or
$validator->addRule('postalCode', new PostalCode( PostalCode::IT ) );

Basic Usage

 $validation = $validator->validate( $_POST,
 [
    'postalCode' => 'required|postalCode'
 ]);

Enforce the default postal code pattern with a default ISO 3166-1 country code parameter : CH, DE, ES, FR, GB, IT, US ... $validation = $validator->validate( $_POST, [ 'postalCode' => 'required|postalCode:IT' ]);

Table of Contents

Constants

COUNTRY  = 'country'
The country parameter.

Properties

$default  : string
The default ISO 3166-1 alpha-2 country code.
$fillableParams  : array<string|int, mixed>
The internal list of fillable parameters.
$message  : string
The internal message pattern.

Methods

__construct()  : mixed
Creates a new PostalCodeRule instance.
check()  : bool
Checks whether the given value satisfies the condition.
country()  : static
The country code of the postalCode regex pattern.

Constants

Properties

$default

The default ISO 3166-1 alpha-2 country code.

protected string $default = \org\iso\ISO3166_1::FR

$fillableParams

The internal list of fillable parameters.

protected array<string|int, mixed> $fillableParams = [self::COUNTRY]

$message

The internal message pattern.

protected string $message = ":attribute is not a valid postal code with the value ':value'."

Methods

__construct()

Creates a new PostalCodeRule instance.

public __construct([array<string|int, mixed>|string|null $country = null ][, string $default = ISO3166_1::FR ]) : mixed
Parameters
$country : array<string|int, mixed>|string|null = null

An ISO 3166-1 alpha-2 country code or a list of valid country codes.

$default : string = ISO3166_1::FR

The default ISO 3166-1 alpha-2 country code (By default ISO3166_1::FR)

check()

Checks whether the given value satisfies the condition.

public check(mixed $value) : bool
Parameters
$value : mixed

The value to check.

Tags
throws
ParameterException
Return values
bool

True if the value satisfies the condition.

country()

The country code of the postalCode regex pattern.

public country(array<string|int, mixed>|string|null $value) : static
Parameters
$value : array<string|int, mixed>|string|null

The country code of the postal code.

Tags
example
$lang = 'it' ;
$validator = new Validator() ;
$validator->setValidator( 'postalCode' , new PostalCodeRule() ) ;
$validation = $validator->validate( [ 'postalCode' => 45 ] , [ 'postalCode' => 'required|postalCode:it' ] ) ;
Return values
static

        
On this page

Search results