CakePHP
  • 文档
    • 书籍
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识和商标
  • 商业解决方案
  • 商品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与
    • 问题 (Github)
    • 烘焙坊
    • 精选资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 新闻稿
    • 领英
    • 优酷
    • 脸书
    • 推特
    • Mastodon
    • 帮助和支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

类验证器

Validator 对象封装了与模型数据验证相关的所有方法,它还提供了一个 API 用于动态更改每个模型字段的验证规则。

实现 ArrayAccess 以轻松修改集合中的规则

命名空间: Cake\Validation
链接: https://book.cakephp.com.cn/5/en/core-libraries/validation.html

常量

  • int
    EMPTY_ALL ¶
    self::EMPTY_STRING | self::EMPTY_ARRAY | self::EMPTY_FILE | self::EMPTY_DATE | self::EMPTY_TIME

    所有 EMPTY_* 标志的组合

  • int
    EMPTY_ARRAY ¶
    2

    用于 allowEmptyFor() 的标志

    当给出一个空数组时,它将被识别为空。

  • int
    EMPTY_DATE ¶
    8

    用于 allowEmptyFor() 的标志

    当给出一个数组时,如果它包含 year 键,并且只有空字符串或空值,它将被识别为空。

  • int
    EMPTY_FILE ¶
    4

    用于 allowEmptyFor() 的标志

    \Psr\Http\Message\UploadedFileInterface::getError() 方法的返回值必须等于 UPLOAD_ERR_NO_FILE。

  • int
    EMPTY_NULL ¶
    0

    用于 allowEmptyFor() 的标志

    当给出 null 时,它将被识别为空。

  • int
    EMPTY_STRING ¶
    1

    用于 allowEmptyFor() 的标志

    当给出一个空字符串时,它将被识别为空。

  • int
    EMPTY_TIME ¶
    16

    用于 allowEmptyFor() 的标志

    当给出一个数组时,如果它包含 hour 键,并且只有空字符串或空值,它将被识别为空。

  • string
    NESTED ¶
    '_nested'

    用于标记使用 addNested() 和 addNestedMany() 创建的嵌套规则

  • string
    WHEN_CREATE ¶
    'create'

    通过使用 'create',您可以在首次创建记录时使字段变为必需。

  • string
    WHEN_UPDATE ¶
    'update'

    通过使用 'update',您可以在更新字段时使字段变为必需。

属性摘要

  • $_allowEmptyFlags protected
    array<string, int>

    包含指定每个对应字段为空的标志。

  • $_allowEmptyMessages protected
    array<string, string>

    包含与检查每个对应字段的空值相关的验证消息。

  • $_defaultProviders protected static
    array<string, object|string>

    一个关联数组,包含用作默认提供程序列表的对象或类

  • $_fields protected
    array<string,Cake\Validation\ValidationSet>

    保存 ValidationSet 对象数组

  • $_presenceMessages protected
    array<string, string>

    包含与检查每个对应字段的存在性相关的验证消息。

  • $_providers protected
    array<string, object|string>

    一个关联数组,包含用于验证的方法的对象或类

  • $_stopOnFailure protected
    bool

    是否将最后一个标志应用于生成的规则。

  • $_useI18n protected
    bool

    是否使用 I18n 函数来翻译默认错误消息

方法摘要

  • __construct() public

    构造函数

  • __debugInfo() public

    获取此对象的打印版本。

  • _canBeEmpty() protected

    返回字段是否可以根据 allowEmpty 留下空白

  • _checkPresence() protected

    如果对传递的规则集的任何验证都应由于数据数组中缺少字段而停止,则返回 false

  • _convertValidatorToArray() protected

    将验证器转换为 fieldName => $settings 数组

  • _processRules() protected

    遍历验证集中的每个规则,并收集执行它们产生的错误

  • add() public

    将新规则添加到字段的规则集中。如果第二个参数是数组,则该字段的规则列表将被第二个参数替换,第三个参数将被忽略。

  • addDefaultProvider() public static

    将对象与名称关联,以便它可以用作默认提供程序。

  • addNested() public

    添加嵌套验证器。

  • addNestedMany() public

    添加嵌套验证器。

  • allowEmptyArray() public

    允许字段为空数组。

  • allowEmptyDate() public

    允许字段为空日期。

  • allowEmptyDateTime() public

    允许字段为空日期/时间。

  • allowEmptyFile() public

    允许字段为空文件。

  • allowEmptyFor() public

    用于指示字段可以为空的低级方法。

  • allowEmptyString() public

    允许字段为空字符串。

  • allowEmptyTime() public

    允许字段为空时间。

  • alphaNumeric() public

    向字段添加字母数字规则。

  • array() public

    添加验证规则以确保字段包含数组。

  • ascii() public

    添加验证规则以确保字段仅包含 ascii 字节

  • asciiAlphaNumeric() public

    向字段添加 ascii 字母数字规则。

  • boolean() public

    向字段添加布尔验证规则。

  • count() public

    返回具有验证规则的字段数量

  • creditCard() public

    向字段添加信用卡规则。

  • date() public

    向字段添加日期格式验证规则。

  • dateTime() public

    向字段添加日期时间格式验证规则。

  • decimal() public

    向字段添加十进制验证规则。

  • email() public

    在字段中添加电子邮件验证规则。

  • enum() public

    在字段中添加一个支持枚举的验证规则。

  • equalToField() public

    添加一个规则,比较一个字段是否等于另一个字段。

  • equals() public

    在字段中添加一个等于比较规则。

  • field() public

    返回一个 ValidationSet 对象,包含一个字段的所有验证规则,如果传入第二个参数为 ValidationSet,则会替换之前定义的任何规则集。

  • getDefaultProvider() public static

    如果存在,则返回以该名称存储的默认提供程序。

  • getDefaultProviders() public static

    获取默认提供程序列表。

  • getIterator() public

    返回要验证的每个字段的迭代器。

  • getNotEmptyMessage() public

    获取字段的非空消息。

  • getProvider() public

    如果存在,则返回以该名称存储的提供程序。

  • getRequiredMessage() public

    获取字段的必需消息。

  • greaterThan() public

    在字段中添加一个大于比较规则。

  • greaterThanField() public

    添加一个规则,比较一个字段是否大于另一个字段。

  • greaterThanOrEqual() public

    在字段中添加一个大于或等于比较规则。

  • greaterThanOrEqualToField() public

    添加一个规则,比较一个字段是否大于或等于另一个字段。

  • hasAtLeast() public

    添加一个验证规则,以确保字段是一个数组,并且包含至少指定数量的元素。

  • hasAtMost() public

    添加一个验证规则,以确保字段是一个数组,并且包含至多指定数量的元素。

  • hasField() public

    检查验证器是否包含给定字段的任何规则。

  • hexColor() public

    添加一个验证规则,以确保字段是一个 6 位十六进制颜色值。

  • inList() public

    添加一个验证规则,以确保字段值在允许的列表中。

  • integer() public

    添加一个验证规则,以确保字段是一个整数值。

  • invertWhenClause() protected

    反转 when 子句,用于创建非空规则。

  • ip() public

    在字段中添加一个 IP 验证规则。

  • ipv4() public

    在字段中添加一个 IPv4 验证规则。

  • ipv6() public

    在字段中添加一个 IPv6 验证规则。

  • isEmpty() protected

    如果字段在传入的数据数组中为空,则返回 true。

  • isEmptyAllowed() public

    返回字段是否可以为空,用于新记录或已存在的记录。

  • isPresenceRequired() public

    返回字段是否可以为空,用于新记录或已存在的记录。

  • latLong() public

    添加一个验证规则,以确保字段是一个纬度/经度元组。

  • latitude() public

    添加一个验证规则,以确保字段是一个纬度。

  • lengthBetween() public

    添加一个规则,以确保字符串长度在一定范围内。

  • lessThan() public

    在字段中添加一个小于比较规则。

  • lessThanField() public

    添加一个规则,比较一个字段是否小于另一个字段。

  • lessThanOrEqual() public

    在字段中添加一个小于或等于比较规则。

  • lessThanOrEqualToField() public

    添加一个规则,比较一个字段是否小于或等于另一个字段。

  • localizedTime() public

    在字段中添加一个本地化时间、日期或日期时间格式验证规则。

  • longitude() public

    添加一个验证规则,以确保字段是一个经度。

  • maxLength() public

    在字段中添加一个字符串长度验证规则。

  • maxLengthBytes() public

    在字段中添加一个字符串长度验证规则。

  • minLength() public

    在字段中添加一个字符串长度验证规则。

  • minLengthBytes() public

    在字段中添加一个字符串长度验证规则。

  • multipleOptions() public

    添加一个多选验证规则,默认情况下比较区分大小写。

  • naturalNumber() public

    在字段中添加一个自然数验证规则。

  • nonNegativeInteger() public

    添加一个验证规则,以确保字段是一个非负整数。

  • notAlphaNumeric() public

    在字段中添加一个非字母数字规则。

  • notAsciiAlphaNumeric() public

    在字段中添加一个非 ASCII 字母数字规则。

  • notBlank() public

    在字段中添加一个非空白规则。

  • notEmptyArray() public

    要求字段是一个非空数组。

  • notEmptyDate() public

    要求一个非空的日期值。

  • notEmptyDateTime() public

    要求字段是一个非空日期/时间。

  • notEmptyFile() public

    要求字段是一个非空文件。

  • notEmptyString() public

    要求字段不是一个空字符串。

  • notEmptyTime() public

    要求字段是一个非空时间。

  • notEqualToField() public

    添加一个规则,比较一个字段是否不等于另一个字段。

  • notEquals() public

    在字段中添加一个不等于比较规则。

  • notSameAs() public

    添加一个规则,比较两个字段的值是否不同。

  • numeric() public

    在字段中添加一个数字值验证规则。

  • offsetExists() public

    返回字段是否定义了规则集。

  • offsetGet() public

    返回字段的规则集。

  • offsetSet() public

    设置字段的规则集。

  • offsetUnset() public

    取消设置字段的规则集。

  • providers() public

    获取此验证器中的提供程序列表。

  • range() public

    添加一个验证规则,以确保字段在数值范围内。

  • regex() public

    返回字段是否与正则表达式匹配。

  • remove() public

    通过名称从集合中删除规则。

  • requirePresence() public

    设置字段是否需要出现在数据数组中,您也可以传递数组,使用数组可以提供以下键。

  • sameAs() public

    添加一个规则,比较两个字段。

  • scalar() public

    添加一个验证规则,以确保字段包含一个标量。

  • setProvider() public

    将一个对象与一个名称相关联,以便它可以用作提供程序。提供程序是对象或类名,它们可以包含在验证期间使用的方法,或者用于确定是否可以应用验证规则。所有验证方法在调用时都会收到存储在此验证器中的完整提供程序列表。

  • setStopOnFailure() public

    是否在第一个验证规则失败时停止验证规则评估。

  • time() public

    在字段中添加一个时间格式验证规则。

  • uploadedFile() public

    添加一个验证规则,以确保字段是一个上传文件。

  • url() public

    添加一个验证规则,以确保字段是一个 URL。

  • urlWithProtocol() public

    添加一个验证规则,以确保字段是一个 URL。

  • utf8() public

    添加一个验证规则,以确保字段只包含 BMP utf8 字节。

  • utf8Extended() public

    添加一个验证规则,以确保字段只包含 utf8 字节。

  • uuid() public

    添加一个验证规则,以确保字段是一个 UUID。

  • validate() public

    验证并返回一个包含失败字段及其错误消息的数组。

方法详情

__construct() ¶ public

__construct()

构造函数

__debugInfo() ¶ public

__debugInfo(): array<string, mixed>

获取此对象的打印版本。

返回
array<string, mixed>

_canBeEmpty() ¶ protected

_canBeEmpty(Cake\Validation\ValidationSet $field, array<string, mixed> $context): bool

返回字段是否可以根据 allowEmpty 留下空白

参数
Cake\Validation\ValidationSet $field

字段的规则集

array<string, mixed> $context

包含验证上下文的键值列表。

返回
bool

_checkPresence() ¶ protected

_checkPresence(Cake\Validation\ValidationSet $field, array<string, mixed> $context): bool

如果对传递的规则集的任何验证都应由于数据数组中缺少字段而停止,则返回 false

参数
Cake\Validation\ValidationSet $field

字段的规则集。

array<string, mixed> $context

包含验证上下文的键值列表。

返回
bool

_convertValidatorToArray() ¶ protected

_convertValidatorToArray(string $fieldName, array<string, mixed> $defaults = [], array<string|int, mixed>|string|int $settings = []): array<string, array<string|int, mixed>>

将验证器转换为 fieldName => $settings 数组

参数
string $fieldName

字段名称

array<string, mixed> $defaults optional

默认设置

array<string|int, mixed>|string|int $settings optional

来自数据的设置

返回
array<string, array<string|int, mixed>>
抛出
InvalidArgumentException

_processRules() ¶ protected

_processRules(string $field, Cake\Validation\ValidationSet $rules, array $data, bool $newRecord): array<string, mixed>

遍历验证集中的每个规则,并收集执行它们产生的错误

参数
string $field

正在处理的字段名称

Cake\Validation\ValidationSet $rules

字段的规则列表

array $data

传递给验证器的完整数据

bool $newRecord

是否为新记录或现有记录

返回
array<string, mixed>

add() ¶ public

add(string $field, array|string $name, Cake\Validation\ValidationRule|array $rule = []): $this

将新规则添加到字段的规则集中。如果第二个参数是数组,则该字段的规则列表将被第二个参数替换,第三个参数将被忽略。

示例

     $validator
         ->add('title', 'required', ['rule' => 'notBlank'])
         ->add('user_id', 'valid', ['rule' => 'numeric', 'message' => 'Invalid User'])

$validator->add('password', [
         'size' => ['rule' => ['lengthBetween', 8, 20]],
         'hasSpecialCharacter' => ['rule' => 'validateSpecialchar', 'message' => 'not valid']
     ]);
参数
string $field

将添加规则的字段名称

array|string $name

单个规则或多个规则数组的别名

Cake\Validation\ValidationRule|array $rule optional

要添加的规则

返回
$this
抛出
InvalidArgumentException
如果数字索引无法解析为字符串索引

addDefaultProvider() ¶ public static

addDefaultProvider(string $name, object|string $object): void

将对象与名称关联,以便它可以用作默认提供程序。

参数
string $name

应设置提供者的名称。

object|string $object

提供者对象或类名。

返回
void

addNested() ¶ public

addNested(string $field, Cake\Validation\Validator $validator, string|null $message = null, Closure|string|null $when = null): $this

添加嵌套验证器。

嵌套验证器允许您为数组类型定义验证器。例如,嵌套验证器非常适合验证子文档或复杂数组类型。

此方法假设子文档与父文档具有 1:1 关系。

当检查错误时,父验证器的提供者将同步到嵌套验证器中。这确保了连接在父验证器中的任何验证规则提供者在规则评估时在嵌套验证器中具有相同的值。

参数
string $field

嵌套验证器的根字段。

Cake\Validation\Validator $validator

嵌套验证器。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this

addNestedMany() ¶ public

addNestedMany(string $field, Cake\Validation\Validator $validator, string|null $message = null, Closure|string|null $when = null): $this

添加嵌套验证器。

嵌套验证器允许您为数组类型定义验证器。例如,嵌套验证器非常适合验证多个类似的子文档或复杂数组类型。

此方法假设子文档与父文档具有 1:N 关系。

当检查错误时,父验证器的提供者将同步到嵌套验证器中。这确保了连接在父验证器中的任何验证规则提供者在规则评估时在嵌套验证器中具有相同的值。

参数
string $field

嵌套验证器的根字段。

Cake\Validation\Validator $validator

嵌套验证器。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this

allowEmptyArray() ¶ public

allowEmptyArray(string $field, string|null $message = null, Closure|string|bool $when = true): $this

允许字段为空数组。

此方法等效于使用 EMPTY_STRING + EMPTY_ARRAY 标志调用 allowEmptyFor()。

参数
string $field

字段名称。

string|null $message optional

如果字段不为空,则显示的消息

Closure|string|bool $when optional

指示何时允许字段为空。有效值为 true、false、'create'、'update'。如果传递闭包,则只有当回调返回 true 时,才允许字段为空。

返回
$this
另请参见
\Cake\Validation\Validator::allowEmptyFor() 以获取示例。

allowEmptyDate() ¶ public

allowEmptyDate(string $field, string|null $message = null, Closure|string|bool $when = true): $this

允许字段为空日期。

空日期值为 null、''、[] 以及所有值均为 '' 且存在 year 键的数组。

参数
string $field

字段名称。

string|null $message optional

如果字段不为空,则显示的消息

Closure|string|bool $when optional

指示何时允许字段为空。有效值为 true、false、'create'、'update'。如果传递闭包,则只有当回调返回 true 时,才允许字段为空。

返回
$this
另请参见
\Cake\Validation\Validator::allowEmptyFor() 以获取示例

allowEmptyDateTime() ¶ public

allowEmptyDateTime(string $field, string|null $message = null, Closure|string|bool $when = true): $this

允许字段为空日期/时间。

空日期值为 null、''、[] 以及所有值均为 '' 且存在 year 和 hour 键的数组。

此方法等效于使用 EMPTY_STRING + EMPTY_DATE + EMPTY_TIME 标志调用 allowEmptyFor()。

参数
string $field

字段名称。

string|null $message optional

如果字段不为空,则显示的消息

Closure|string|bool $when optional

指示何时允许字段为空。有效值为 true、false、'create'、'update'。如果传递闭包,则只有当回调返回 false 时,才允许字段为空。

返回
$this
另请参见
\Cake\Validation\Validator::allowEmptyFor() 以获取示例。

allowEmptyFile() ¶ public

allowEmptyFile(string $field, string|null $message = null, Closure|string|bool $when = true): $this

允许字段为空文件。

此方法等效于使用 EMPTY_FILE 标志调用 allowEmptyFor()。文件字段不会接受 '' 或 [] 作为空值。只有 null 和错误等于 UPLOAD_ERR_NO_FILE 的文件上传将被视为空。

参数
string $field

字段名称。

string|null $message optional

如果字段不为空,则显示的消息

Closure|string|bool $when optional

指示何时允许字段为空。有效值为 true、'create'、'update'。如果传递闭包,则只有当回调返回 true 时,才允许字段为空。

返回
$this
另请参见
\Cake\Validation\Validator::allowEmptyFor() 有关详细用法

allowEmptyFor() ¶ public

allowEmptyFor(string $field, int|null $flags = null, Closure|string|bool $when = true, string|null $message = null): $this

用于指示字段可以为空的低级方法。

此方法通常不应使用,而应使用

  • allowEmptyString()
  • allowEmptyArray()
  • allowEmptyFile()
  • allowEmptyDate()
  • allowEmptyDatetime()
  • allowEmptyTime()

因为它们的 API 更容易操作和阅读。

您也可以为所有传递的字段设置标志、何时和消息,单个设置优先于组设置。

示例

// Email can be empty
$validator->allowEmptyFor('email', Validator::EMPTY_STRING);

// Email can be empty on create
$validator->allowEmptyFor('email', Validator::EMPTY_STRING, Validator::WHEN_CREATE);

// Email can be empty on update
$validator->allowEmptyFor('email', Validator::EMPTY_STRING, Validator::WHEN_UPDATE);

可以通过将回调作为第二个参数传递来有条件地允许字段为空。回调将接收验证上下文数组作为参数

$validator->allowEmpty('email', Validator::EMPTY_STRING, function ($context) {
  return !$context['newRecord'] || $context['data']['role'] === 'admin';
});

如果您想在字段上允许其他类型的空数据,您需要传递其他标志

$validator->allowEmptyFor('photo', Validator::EMPTY_FILE);
$validator->allowEmptyFor('published', Validator::EMPTY_STRING | Validator::EMPTY_DATE | Validator::EMPTY_TIME);
$validator->allowEmptyFor('items', Validator::EMPTY_STRING | Validator::EMPTY_ARRAY);

您也可以使用此方法的便捷包装器。以下调用与以上相同

$validator->allowEmptyFile('photo');
$validator->allowEmptyDateTime('published');
$validator->allowEmptyArray('items');
参数
string $field

字段名称。

int|null $flags optional

EMPTY_* 标志的位掩码,指定哪些为空。如果没有提供标志/位掩码,则只有 null 将被允许为空值。

Closure|string|bool $when optional

指示何时允许字段为空。有效值为 true、false、'create'、'update'。如果传递闭包,则只有当回调返回 true 时,才允许字段为空。

string|null $message optional

如果字段不为空,则显示的消息

返回
$this

allowEmptyString() ¶ public

allowEmptyString(string $field, string|null $message = null, Closure|string|bool $when = true): $this

允许字段为空字符串。

此方法等效于使用 EMPTY_STRING 标志调用 allowEmptyFor()。

参数
string $field

字段名称。

string|null $message optional

如果字段不为空,则显示的消息

Closure|string|bool $when optional

指示何时允许字段为空。有效值为 true、false、'create'、'update'。如果传递闭包,则只有当回调返回 true 时,才允许字段为空。

返回
$this
另请参见
\Cake\Validation\Validator::allowEmptyFor() 有关详细用法

allowEmptyTime() ¶ public

allowEmptyTime(string $field, string|null $message = null, Closure|string|bool $when = true): $this

允许字段为空时间。

空日期值为 null、''、[] 以及所有值均为 '' 且存在 hour 键的数组。

此方法等效于使用 EMPTY_STRING + EMPTY_TIME 标志调用 allowEmptyFor()。

参数
string $field

字段名称。

string|null $message optional

如果字段不为空,则显示的消息

Closure|string|bool $when optional

指示何时允许字段为空。有效值为 true、false、'create'、'update'。如果传递闭包,则只有当回调返回 true 时,才允许字段为空。

返回
$this
另请参见
\Cake\Validation\Validator::allowEmptyFor() 以获取示例。

alphaNumeric() ¶ public

alphaNumeric(string $field, string|null $message = null, Closure|string|null $when = null): $this

向字段添加字母数字规则。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::alphaNumeric()

array() ¶ public

array(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加验证规则以确保字段包含数组。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::isArray()

ascii() ¶ public

ascii(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加验证规则以确保字段仅包含 ascii 字节

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::ascii()

asciiAlphaNumeric() ¶ public

asciiAlphaNumeric(string $field, string|null $message = null, Closure|string|null $when = null): $this

向字段添加 ascii 字母数字规则。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::asciiAlphaNumeric()

boolean() ¶ public

boolean(string $field, string|null $message = null, Closure|string|null $when = null): $this

向字段添加布尔验证规则。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::boolean()

count() ¶ public

count(): int

返回具有验证规则的字段数量

返回
int

creditCard() ¶ public

creditCard(string $field, array|string $type = 'all', string|null $message = null, Closure|string|null $when = null): $this

向字段添加信用卡规则。

参数
string $field

要应用规则的字段。

array|string $type 可选

要允许的卡类型。默认值为 'all'。您也可以提供一个接受的卡类型数组。例如 ['mastercard', 'visa', 'amex']

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::creditCard()

date() ¶ public

date(string $field, list<string> $formats = ['ymd'], string|null $message = null, Closure|string|null $when = null): $this

向字段添加日期格式验证规则。

参数
string $field

要应用规则的字段。

list<string> $formats 可选

接受的日期格式列表。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::date()

dateTime() ¶ public

dateTime(string $field, list<string> $formats = ['ymd'], string|null $message = null, Closure|string|null $when = null): $this

向字段添加日期时间格式验证规则。

参数
string $field

要应用规则的字段。

list<string> $formats 可选

接受的日期格式列表。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::datetime()

decimal() ¶ public

decimal(string $field, int|null $places = null, string|null $message = null, Closure|string|null $when = null): $this

向字段添加十进制验证规则。

参数
string $field

要应用规则的字段。

int|null $places 可选

所需的十进制位数。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::decimal()

email() ¶ public

email(string $field, bool $checkMX = false, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加电子邮件验证规则。

参数
string $field

要应用规则的字段。

bool $checkMX 可选

是否检查 MX 记录。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::email()

enum() ¶ public

enum(string $field, class-stringBackedEnum> $enumClassName, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个支持枚举的验证规则。

参数
string $field

要应用规则的字段。

class-stringBackedEnum> $enumClassName

有效的支持枚举类名。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::enum()

equalToField() ¶ public

equalToField(string $field, string $secondField, string|null $message = null, Closure|string|null $when = null): $this

添加一个规则,比较一个字段是否等于另一个字段。

参数
string $field

要应用规则的字段。

string $secondField

要比较的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::compareFields()

equals() ¶ public

equals(string $field, mixed $value, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个等于比较规则。

参数
string $field

要应用规则的字段。

mixed $value

用户数据必须等于的值。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::comparison()

field() ¶ public

field(string $name, Cake\Validation\ValidationSet|null $set = null): Cake\Validation\ValidationSet

返回一个 ValidationSet 对象,包含一个字段的所有验证规则,如果传入第二个参数为 ValidationSet,则会替换之前定义的任何规则集。

参数
string $name

[可选] 要获取的字段名。

Cake\Validation\ValidationSet|null $set 可选

字段的规则集

返回
Cake\Validation\ValidationSet

getDefaultProvider() ¶ public static

getDefaultProvider(string $name): object|class-string|null

如果存在,则返回以该名称存储的默认提供程序。

参数
string $name

应该检索提供者的名称。

返回
object|class-string|null

getDefaultProviders() ¶ public static

getDefaultProviders(): list<string>

获取默认提供程序列表。

返回
list<string>

getIterator() ¶ public

getIterator(): Traversable<string,Cake\Validation\ValidationSet>

返回要验证的每个字段的迭代器。

返回
Traversable<string,Cake\Validation\ValidationSet>

getNotEmptyMessage() ¶ public

getNotEmptyMessage(string $field): string|null

获取字段的非空消息。

参数
string $field

字段名称

返回
string|null

getProvider() ¶ public

getProvider(string $name): object|class-string|null

如果存在,则返回以该名称存储的提供程序。

参数
string $name

应设置提供者的名称。

返回
object|class-string|null

getRequiredMessage() ¶ public

getRequiredMessage(string $field): string|null

获取字段的必需消息。

参数
string $field

字段名称

返回
string|null

greaterThan() ¶ public

greaterThan(string $field, float|int $value, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个大于比较规则。

参数
string $field

要应用规则的字段。

float|int $value

用户数据必须大于的值。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::comparison()

greaterThanField() ¶ public

greaterThanField(string $field, string $secondField, string|null $message = null, Closure|string|null $when = null): $this

添加一个规则,比较一个字段是否大于另一个字段。

参数
string $field

要应用规则的字段。

string $secondField

要比较的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::compareFields()

greaterThanOrEqual() ¶ public

greaterThanOrEqual(string $field, float|int $value, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个大于或等于比较规则。

参数
string $field

要应用规则的字段。

float|int $value

用户数据必须大于或等于的值。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::comparison()

greaterThanOrEqualToField() ¶ public

greaterThanOrEqualToField(string $field, string $secondField, string|null $message = null, Closure|string|null $when = null): $this

添加一个规则,比较一个字段是否大于或等于另一个字段。

参数
string $field

要应用规则的字段。

string $secondField

要比较的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::compareFields()

hasAtLeast() ¶ public

hasAtLeast(string $field, int $count, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个数组,并且包含至少指定数量的元素。

参数
string $field

要应用规则的字段。

int $count

数组至少应包含的元素数量

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::numElements()

hasAtMost() ¶ public

hasAtMost(string $field, int $count, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个数组,并且包含至多指定数量的元素。

参数
string $field

要应用规则的字段。

int $count

字段应该包含的元素的最大数量

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::numElements()

hasField() ¶ public

hasField(string $name): bool

检查验证器是否包含给定字段的任何规则。

参数
string $name

要检查的字段名称。

返回
bool

hexColor() ¶ public

hexColor(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个 6 位十六进制颜色值。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::hexColor()

inList() ¶ public

inList(string $field, array $list, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段值在允许的列表中。

参数
string $field

要应用规则的字段。

array $list

有效的选项列表。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::inList()

integer() ¶ public

integer(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个整数值。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::isInteger()

invertWhenClause() ¶ protected

invertWhenClause(Closure|string|bool $when): Closure|string|bool

反转 when 子句,用于创建非空规则。

参数
Closure|string|bool $when

指示字段何时不允许为空。有效值为 true(始终)、'create'、'update'。如果传递了 Closure,则只有当回调返回 false 时,字段才允许为空。

返回
Closure|string|bool

ip() ¶ public

ip(string $field, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个 IP 验证规则。

此规则将接受 IPv4 和 IPv6 地址。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::ip()

ipv4() ¶ public

ipv4(string $field, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个 IPv4 验证规则。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::ip()

ipv6() ¶ public

ipv6(string $field, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个 IPv6 验证规则。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::ip()

isEmpty() ¶ protected

isEmpty(mixed $data, int $flags): bool

如果字段在传入的数据数组中为空,则返回 true。

参数
mixed $data

要检查的值。

int $flags

EMPTY_* 标志的位掩码,指定哪些为空

返回
bool

isEmptyAllowed() ¶ public

isEmptyAllowed(string $field, bool $newRecord): bool

返回字段是否可以为空,用于新记录或已存在的记录。

参数
string $field

字段名称。

bool $newRecord

要验证的数据是新的还是要更新。

返回
bool

isPresenceRequired() ¶ public

isPresenceRequired(string $field, bool $newRecord): bool

返回字段是否可以为空,用于新记录或已存在的记录。

参数
string $field

字段名称。

bool $newRecord

要验证的数据是新的还是要更新。

返回
bool

latLong() ¶ public

latLong(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个纬度/经度元组。

例如 <lat>, <lng>

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::geoCoordinate()

latitude() ¶ public

latitude(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个纬度。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::latitude()

lengthBetween() ¶ public

lengthBetween(string $field, array $range, string|null $message = null, Closure|string|null $when = null): $this

添加一个规则,以确保字符串长度在一定范围内。

参数
string $field

要应用规则的字段。

array $range

您要允许的包含最小值和最大值的长度。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
抛出
InvalidArgumentException
另请参见
\Cake\Validation\Validation::alphaNumeric()

lessThan() ¶ public

lessThan(string $field, float|int $value, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个小于比较规则。

参数
string $field

要应用规则的字段。

float|int $value

用户数据的值必须小于此值。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::comparison()

lessThanField() ¶ public

lessThanField(string $field, string $secondField, string|null $message = null, Closure|string|null $when = null): $this

添加一个规则,比较一个字段是否小于另一个字段。

参数
string $field

要应用规则的字段。

string $secondField

要比较的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::compareFields()

lessThanOrEqual() ¶ public

lessThanOrEqual(string $field, float|int $value, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个小于或等于比较规则。

参数
string $field

要应用规则的字段。

float|int $value

用户数据的值必须小于或等于此值。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::comparison()

lessThanOrEqualToField() ¶ public

lessThanOrEqualToField(string $field, string $secondField, string|null $message = null, Closure|string|null $when = null): $this

添加一个规则,比较一个字段是否小于或等于另一个字段。

参数
string $field

要应用规则的字段。

string $secondField

要比较的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::compareFields()

localizedTime() ¶ public

localizedTime(string $field, string $type = 'datetime', string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个本地化时间、日期或日期时间格式验证规则。

参数
string $field

要应用规则的字段。

string $type 可选

解析器类型,可以选择 'date'、'time' 和 'datetime' 之一

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::localizedTime()

longitude() ¶ public

longitude(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个经度。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::longitude()

maxLength() ¶ public

maxLength(string $field, int $max, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个字符串长度验证规则。

参数
string $field

要应用规则的字段。

int $max

允许的最大长度。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::maxLength()

maxLengthBytes() ¶ public

maxLengthBytes(string $field, int $max, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个字符串长度验证规则。

参数
string $field

要应用规则的字段。

int $max

允许的最大长度。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::maxLengthBytes()

minLength() ¶ public

minLength(string $field, int $min, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个字符串长度验证规则。

参数
string $field

要应用规则的字段。

int $min

所需的最小长度。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::minLength()

minLengthBytes() ¶ public

minLengthBytes(string $field, int $min, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个字符串长度验证规则。

参数
string $field

要应用规则的字段。

int $min

所需的最小长度。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::minLengthBytes()

multipleOptions() ¶ public

multipleOptions(string $field, array<string, mixed> $options = [], string|null $message = null, Closure|string|null $when = null): $this

添加一个多选验证规则,默认情况下比较区分大小写。

参数
string $field

要应用规则的字段。

array<string, mixed> $options 可选

验证器的选项。包括在 \Cake\Validation\Validation::multiple() 中定义的选项和 caseInsensitive 参数。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::multiple()

naturalNumber() ¶ public

naturalNumber(string $field, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个自然数验证规则。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::naturalNumber()

nonNegativeInteger() ¶ public

nonNegativeInteger(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个非负整数。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::naturalNumber()

notAlphaNumeric() ¶ public

notAlphaNumeric(string $field, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个非字母数字规则。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::notAlphaNumeric()

notAsciiAlphaNumeric() ¶ public

notAsciiAlphaNumeric(string $field, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个非 ASCII 字母数字规则。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::notAlphaNumeric()

notBlank() ¶ public

notBlank(string $field, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个非空白规则。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::notBlank()

notEmptyArray() ¶ public

notEmptyArray(string $field, string|null $message = null, Closure|string|bool $when = false): $this

要求字段是一个非空数组。

allowEmptyArray() 的反面

参数
string $field

字段名称。

string|null $message optional

如果字段为空,要显示的消息。

Closure|string|bool $when optional

指示字段不允许为空。有效值为 false(永不允许)、'create'、'update'。如果传递了一个闭包,则只有在回调返回 true 时,该字段才需要不为空。

返回
$this
另请参见
\Cake\Validation\Validator::allowEmptyArray()

notEmptyDate() ¶ public

notEmptyDate(string $field, string|null $message = null, Closure|string|bool $when = false): $this

要求一个非空的日期值。

参数
string $field

字段名称。

string|null $message optional

如果字段为空,要显示的消息。

Closure|string|bool $when optional

指示字段不允许为空。有效值为 false(永不允许)、'create'、'update'。如果传递了一个闭包,则只有在回调返回 true 时,该字段才需要不为空。

返回
$this
另请参见
有关示例,请参阅 \Cake\Validation\Validator::allowEmptyDate()

notEmptyDateTime() ¶ public

notEmptyDateTime(string $field, string|null $message = null, Closure|string|bool $when = false): $this

要求字段是一个非空日期/时间。

allowEmptyDateTime 的反面

参数
string $field

字段名称。

string|null $message optional

如果字段为空,要显示的消息。

Closure|string|bool $when optional

指示字段不允许为空。有效值为 false(永不允许)、'create'、'update'。如果传递了一个闭包,则只有在回调返回 true 时,该字段才需要不为空。

返回
$this
另请参见
\Cake\Validation\Validator::allowEmptyDateTime()

notEmptyFile() ¶ public

notEmptyFile(string $field, string|null $message = null, Closure|string|bool $when = false): $this

要求字段是一个非空文件。

allowEmptyFile() 的反面

参数
string $field

字段名称。

string|null $message optional

如果字段为空,要显示的消息。

Closure|string|bool $when optional

指示字段不允许为空。有效值为 false(永不允许)、'create'、'update'。如果传递了一个闭包,则只有在回调返回 true 时,该字段才需要不为空。

返回
$this
另请参见
\Cake\Validation\Validator::allowEmptyFile()

notEmptyString() ¶ public

notEmptyString(string $field, string|null $message = null, Closure|string|bool $when = false): $this

要求字段不是一个空字符串。

allowEmptyString() 的反面

参数
string $field

字段名称。

string|null $message optional

如果字段为空,要显示的消息。

Closure|string|bool $when optional

指示字段不允许为空。有效值为 false(永不允许)、'create'、'update'。如果传递了一个闭包,则只有在回调返回 true 时,该字段才需要不为空。

返回
$this
另请参见
\Cake\Validation\Validator::allowEmptyString()

notEmptyTime() ¶ public

notEmptyTime(string $field, string|null $message = null, Closure|string|bool $when = false): $this

要求字段是一个非空时间。

allowEmptyTime() 的反面

参数
string $field

字段名称。

string|null $message optional

如果字段为空,要显示的消息。

Closure|string|bool $when optional

指示字段不允许为空。有效值为 false(永不允许)、'create'、'update'。如果传递了一个闭包,则只有在回调返回 true 时,该字段才需要不为空。

返回
$this
另请参见
\Cake\Validation\Validator::allowEmptyTime()

notEqualToField() ¶ public

notEqualToField(string $field, string $secondField, string|null $message = null, Closure|string|null $when = null): $this

添加一个规则,比较一个字段是否不等于另一个字段。

参数
string $field

要应用规则的字段。

string $secondField

要比较的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::compareFields()

notEquals() ¶ public

notEquals(string $field, mixed $value, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个不等于比较规则。

参数
string $field

要应用规则的字段。

mixed $value

用户数据的值不能等于此值。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::comparison()

notSameAs() ¶ public

notSameAs(string $field, string $secondField, string|null $message = null, Closure|string|null $when = null): $this

添加一个规则,比较两个字段的值是否不同。

参数
string $field

要应用规则的字段。

string $secondField

要比较的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::compareFields()

numeric() ¶ public

numeric(string $field, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个数字值验证规则。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::numeric()

offsetExists() ¶ public

offsetExists(string $field): bool

返回字段是否定义了规则集。

参数
string $field

要检查的字段名称

返回
bool

offsetGet() ¶ public

offsetGet(string|int $field): Cake\Validation\ValidationSet

返回字段的规则集。

参数
string|int $field

要检查的字段名称

返回
Cake\Validation\ValidationSet

offsetSet() ¶ public

offsetSet(string $offset, Cake\Validation\ValidationSet|array $value): void

设置字段的规则集。

参数
string $offset

要设置的字段名称

Cake\Validation\ValidationSet|array $value

要应用于字段的一组规则

返回
void

offsetUnset() ¶ public

offsetUnset(string $field): void

取消设置字段的规则集。

参数
string $field

要取消设置的字段名称

返回
void

providers() ¶ public

providers(): list<string>

获取此验证器中的提供程序列表。

返回
list<string>

range() ¶ public

range(string $field, array $range, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段在数值范围内。

参数
string $field

要应用规则的字段。

array $range

有效范围的包含上界和下界。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
抛出
InvalidArgumentException
另请参见
\Cake\Validation\Validation::range()

regex() ¶ public

regex(string $field, string $regex, string|null $message = null, Closure|string|null $when = null): $this

返回字段是否与正则表达式匹配。

参数
string $field

字段名称。

string $regex

正则表达式。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this

remove() ¶ public

remove(string $field, string|null $rule = null): $this

通过名称从集合中删除规则。

示例

     $validator
         ->remove('title', 'required')
         ->remove('user_id')
参数
string $field

要从中移除规则的字段名称

string|null $rule 可选

要移除的规则名称

返回
$this

requirePresence() ¶ public

requirePresence(array<string|int, mixed>|string $field, Closure|string|bool $mode = true, string|null $message = null): $this

设置字段是否需要出现在数据数组中,您也可以传递数组,使用数组可以提供以下键。

  • mode 字段的单独模式
  • message 字段的单独错误消息

您还可以为所有传递的字段设置模式和消息,单个设置优先于组设置。

参数
array<string|int, mixed>|string $field

字段名称或字段列表。

Closure|string|bool $mode 可选

有效值为 true、false、'create'、'update'。如果传递了一个闭包,则只有在回调返回 true 时,该字段才需要存在。

string|null $message optional

如果字段存在性验证失败,要显示的消息。

返回
$this

sameAs() ¶ public

sameAs(string $field, string $secondField, string|null $message = null, Closure|string|null $when = null): $this

添加一个规则,比较两个字段。

如果两个字段的值完全相同,则规则将通过。

参数
string $field

要应用规则的字段。

string $secondField

要比较的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::compareFields()

scalar() ¶ public

scalar(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段包含一个标量。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::isScalar()

setProvider() ¶ public

setProvider(string $name, object|string $object): $this

将一个对象与一个名称相关联,以便它可以用作提供程序。提供程序是对象或类名,它们可以包含在验证期间使用的方法,或者用于确定是否可以应用验证规则。所有验证方法在调用时都会收到存储在此验证器中的完整提供程序列表。

参数
string $name

应设置提供者的名称。

object|string $object

提供者对象或类名。

返回
$this

setStopOnFailure() ¶ public

setStopOnFailure(bool $stopOnFailure = true): $this

是否在第一个验证规则失败时停止验证规则评估。

启用此功能后,每个字段的第一个失败规则将导致验证停止。禁用此功能后,即使存在失败,也会运行所有规则。

参数
bool $stopOnFailure 可选

是否应用 last 标志。

返回
$this

time() ¶ public

time(string $field, string|null $message = null, Closure|string|null $when = null): $this

在字段中添加一个时间格式验证规则。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::time()

uploadedFile() ¶ public

uploadedFile(string $field, array<string, mixed> $options, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个上传文件。

参数
string $field

要应用规则的字段。

array<string, mixed> $options

选项数组。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::uploadedFile() 用于选项

url() ¶ public

url(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个 URL。

此验证器不需要协议。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::url()

urlWithProtocol() ¶ public

urlWithProtocol(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个 URL。

此验证器要求 URL 具有协议。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::url()

utf8() ¶ public

utf8(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段只包含 BMP utf8 字节。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::utf8()

utf8Extended() ¶ public

utf8Extended(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段只包含 utf8 字节。

此规则将接受 3 字节和 4 字节的 UTF8 序列,这对表情符号是必需的。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::utf8()

uuid() ¶ public

uuid(string $field, string|null $message = null, Closure|string|null $when = null): $this

添加一个验证规则,以确保字段是一个 UUID。

参数
string $field

要应用规则的字段。

string|null $message optional

规则失败时的错误消息。

Closure|string|null $when optional

可以是 'create' 或 'update',也可以是当应应用验证规则时返回 true 的闭包。

返回
$this
另请参见
\Cake\Validation\Validation::uuid()

validate() ¶ public

validate(array $data, bool $newRecord = true): array<array>

验证并返回一个包含失败字段及其错误消息的数组。

参数
array $data

要检查错误的数据

bool $newRecord 可选

要验证的数据是新的还是要更新。

返回
array<array>

属性详细信息

$_allowEmptyFlags ¶ protected

包含指定每个对应字段为空的标志。

类型
array<string, int>

$_allowEmptyMessages ¶ protected

包含与检查每个对应字段的空值相关的验证消息。

类型
array<string, string>

$_defaultProviders ¶ protected static

一个关联数组,包含用作默认提供程序列表的对象或类

类型
array<string, object|string>

$_fields ¶ protected

保存 ValidationSet 对象数组

类型
array<string,Cake\Validation\ValidationSet>

$_presenceMessages ¶ protected

包含与检查每个对应字段的存在性相关的验证消息。

类型
array<string, string>

$_providers ¶ protected

一个关联数组,包含用于验证的方法的对象或类

类型
array<string, object|string>

$_stopOnFailure ¶ protected

是否将最后一个标志应用于生成的规则。

类型
bool

$_useI18n ¶ protected

是否使用 I18n 函数来翻译默认错误消息

类型
bool
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 书籍
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识和商标
  • 社区
  • 参与
  • 问题 (Github)
  • 烘焙坊
  • 精选资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 新闻稿
  • 领英
  • 优酷
  • 脸书
  • 推特
  • Mastodon
  • 帮助和支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成