CakePHP-検証
多くの場合、Webサイトの作成中に、データをさらに処理する前に特定のことを検証する必要があります。 CakePHPは、簡単にデータを検証できるバリデーターを構築するための検証パッケージを提供します。
検証方法
CakePHPは、検証クラスでさまざまな検証メソッドを提供します。 最も人気のあるもののいくつかを以下に示します。
Syntax
|
Add (string $field, array
|
string $name, array
|
Cake\Validation\ValidationRule $rule [] )
|
Parameters
|
- ルールが追加されるフィールドの名前。
- 単一のルールまたは複数のルール配列のエイリアス。
- 追加するルール
|
Returns
|
$this
|
Description
|
Adds a new rule to a field’s rule set. If second argument is an array, then rules list for the field will be replaced with second argument and third argument will be ignored.
|
Syntax
|
allowEmpty (string $field, boolean
|
string
|
callable $when true, string
|
null $message null)
|
Parameters
|
- フィールドの名前。
- フィールドをいつ空にすることができるかを示します。 有効な値は true(常に)、 'create'、 'update' です。 callableが渡された場合、フィールドはコールバックがtrueを返す場合にのみ空のままになります。
- フィールドがそうでない場合に表示するメッセージ。
|
Returns
|
$this
|
Description
|
Syntax
|
alphanumeric (string $field, string
|
null $message null, string
|
callable
|
null $when null)
|
Parameters
|
- ルールを適用するフィールド。
- ルールが失敗したときのエラーメッセージ。
- 「作成」または「更新」、または検証ルールを適用する必要があるときにtrueを返す呼び出し可能オブジェクトのいずれか。
|
Returns
|
$this
|
Description
|
Syntax
|
creditCard (string $field, string $type 'all', string
|
null $message null, string
|
callable
|
null $when null)
|
Parameters
|
- ルールを適用するフィールド。
- 許可するカードのタイプ。 デフォルトは「all」です。 また、たとえば['mastercard'、 'visa'、 'amex']など、受け入れられたカードタイプの配列を指定することもできます。
- ルールが失敗したときのエラーメッセージ。
- 「作成」または「更新」、または検証ルールを適用する必要があるときにtrueを返す呼び出し可能オブジェクトのいずれか。
|
Returns
|
$this
|
Description
|
Syntax
|
Email (string $field, boolean $checkMX false, string
|
null $message null, string
|
callable
|
null $when null)
|
Parameters
|
- ルールを適用するフィールド。
- MXレコードを確認するかどうか。
- ルールが失敗したときのエラーメッセージ。
- 「作成」または「更新」、または検証ルールを適用する必要があるときにtrueを返す呼び出し可能オブジェクトのいずれか。
|
Returns
|
$this
|
Description
|
Syntax
|
maxLength (string $field, integer $max, string
|
null $message null, string
|
callable
|
null $when null)
|
Parameters
|
- ルールを適用するフィールド。
- 許可される最大長。
- ルールが失敗したときのエラーメッセージ。
- 'create' または 'update' 、または検証ルールを適用する必要があるときにtrueを返す callable のいずれか。
|
Returns
|
$this
|
Description
|
Syntax
|
minLength (string $field, integer $min, string
|
null $message null, string
|
callable
|
null $when null)
|
Parameters
|
- ルールを適用するフィールド。
- 許可される最大長。
- ルールが失敗したときのエラーメッセージ。
- 'create' または 'update' 、または検証ルールを適用する必要があるときにtrueを返す callable のいずれか。
|
Returns
|
$this
|
Description
|
Syntax
|
notBlank (string $field, string
|
null $message null, string
|
callable
|
null $when null)
|
Parameters
|
- ルールを適用するフィールド。
- ルールが失敗したときのエラーメッセージ。
- 'create' または 'update' 、または検証ルールを適用する必要があるときにtrueを返す callable のいずれか。
|
Returns
|
$this
|
Description
|