NoraLib Validation API Documentation

Validator
in package

バリデーションルールを構築するためのファクトリークラス

このクラスは、様々なバリデーションルールを簡単に組み合わせて 使用するためのインターフェースを提供します。

Tags
example

基本的な使い方

$validator = new Validator();
$emailValidator = $validator->validEmail();
if ($emailValidator->validate('user@example.com')) {
    echo "有効なメールアドレス";
}
example

ルールの組み合わせ

$validator = new Validator();
$passwordValidator = $validator
    ->notEmpty()
    ->length(min: 8, max: 20)
    ->passwordStrength();
see
RootValidator
see
AbstractRule
author

Hajime MATSUMOTO

license

MIT

Table of Contents

Properties

$policy  : array<string|int, mixed>

Methods

__construct()  : mixed
allOf()  : RootValidator
anyOf()  : RootValidator
array()  : RootValidator
custom()  : RootValidator
each()  : RootValidator
filter()  : RootValidator
getPolicy()  : array<string|int, AbstractRule>
定義済みのポリシーを取得します
length()  : RootValidator
notEmpty()  : RootValidator
oneOf()  : RootValidator
passwordComplexity()  : RootValidator
passwordNoCommonWord()  : RootValidator
passwordStrength()  : RootValidator
policy()  : RootValidator
regex()  : RootValidator
setPolicy()  : $this
バリデーションポリシーを定義します
validEmail()  : RootValidator
validUrl()  : RootValidator

Properties

$policy

public array<string|int, mixed> $policy = []

Methods

__construct()

public __construct([array<string|int, mixed> $policy = [] ]) : mixed
Parameters
$policy : array<string|int, mixed> = []

allOf()

public allOf(array<string|int, mixed> $rules[, string $message = = '\'\'' ]) : RootValidator

すべてのルールが成功(論理AND)

Parameters
$rules : array<string|int, mixed>
$message : string = = '\'\''
Return values
RootValidator

anyOf()

public anyOf(array<string|int, mixed> $rules[, string $message = = '\'\'' ]) : RootValidator

いずれか1つ以上のルールが成功(論理OR)

Parameters
$rules : array<string|int, mixed>
$message : string = = '\'\''
Return values
RootValidator

array()

public array(array<string|int, mixed> $spec) : RootValidator

配列の各要素をバリデーション

Parameters
$spec : array<string|int, mixed>
Return values
RootValidator

custom()

public custom(Closure $callback[, string $message = = '\'\'' ]) : RootValidator

カスタムバリデーション

Parameters
$callback : Closure
$message : string = = '\'\''
Return values
RootValidator

each()

public each(RuleInterface $rule[, string $message = = '\'\'' ]) : RootValidator

配列の各要素に同じルールを適用

Parameters
$rule : RuleInterface
$message : string = = '\'\''
Return values
RootValidator

getPolicy()

定義済みのポリシーを取得します

public getPolicy(string $name) : array<string|int, AbstractRule>
Parameters
$name : string

ポリシー名

Return values
array<string|int, AbstractRule>

length()

public length(int $min[, int|null $max = = 'null' ][, string $message = = '""' ]) : RootValidator

文字数をチェック

Parameters
$min : int
$max : int|null = = 'null'
$message : string = = '""'
Return values
RootValidator

oneOf()

public oneOf(array<string|int, mixed> $rules[, string $message = = '\'\'' ]) : RootValidator

いずれか1つのルールのみが成功(排他的OR)

Parameters
$rules : array<string|int, mixed>
$message : string = = '\'\''
Return values
RootValidator

passwordComplexity()

public passwordComplexity([int $complexity = = '4' ][, string $special = = '\'\'' ]) : RootValidator

パスワード複雑性をチェック

Parameters
$complexity : int = = '4'
$special : string = = '\'\''
Return values
RootValidator

passwordStrength()

public passwordStrength([int $min = = '8' ][, int $complexity = = '3' ][, string $special = = '\'@!#$%^&*(),.?":{}|<>\'' ][, string $message = = '\'\'' ]) : RootValidator

パスワード強度をチェック

Parameters
$min : int = = '8'
$complexity : int = = '3'
$special : string = = '\'@!#$%^&*(),.?":{}|<>\''
$message : string = = '\'\''
Return values
RootValidator

regex()

public regex(string $pattern[, string $message = = '\'\'' ]) : RootValidator

正規表現マッチング

Parameters
$pattern : string
$message : string = = '\'\''
Return values
RootValidator

setPolicy()

バリデーションポリシーを定義します

public setPolicy(string $name, array<string|int, AbstractRule$rules) : $this

複数のルールを組み合わせたポリシーを定義し、再利用できます。

Parameters
$name : string

ポリシー名

$rules : array<string|int, AbstractRule>

ルールの配列

Tags
example

ポリシーの定義

$validator = new Validator();
$validator->setPolicy('strong_password', [
    new LengthRule(8, 20),
    new PasswordComplexityRule(3),
    new PasswordNoCommonWordRule(),
]);

// 使用
$passwordValidator = $validator->policy('strong_password');
Return values
$this

        
On this page

Search results