CakePHP教程

CakePHP 验证

通常在制作网站时,我们需要在进一步处理数据之前验证某些事情。 CakePHP 提供验证包,用于构建可以轻松验证数据的验证器。

验证方法

CakePHP 在验证类中提供了各种验证方法。下面列出了其中一些最受欢迎的。
语法
Add(string $field, array|string $name, array|Cake\Validation\ValidationRule $rule [] )
参数 将从中添加规则的字段的名称。 单个规则或多个规则数组的别名。 要添加的规则
返回
$this
描述
向字段的规则集添加新规则。如果第二个参数是数组,则该字段的规则列表将替换为第二个参数,第三个参数将被忽略。
语法
allowEmpty(string $field, boolean|string|callable $whentrue, string|null $messagenull)
参数 字段的名称。 指示该字段何时允许为空。有效值为 true(始终)、'create'、'update'。如果传递了可调用对象,则仅当回调返回 true 时,该字段才会留空。 该字段不存在时显示的消息。
返回 $this
描述
允许字段为空。
语法
alphanumeric (string $field, string|null $messagenull, string|callable|null $whennull)
参数 要应用规则的字段。 规则失败时的错误消息。 'create' 或 'update' 或在应应用验证规则时返回 true 的可调用对象。
返回
$this
描述
向字段添加字母数字规则。
语法
creditCard(string $field , string $type'all', string|null $messagenull, string|callable |null $whennull)
参数 要应用规则的字段。 您要允许的卡片类型。默认为"全部"。您还可以提供一系列可接受的卡类型,例如 ['mastercard', 'visa', 'amex']。 规则失败时的错误消息。 'create' 或 'update' 或返回 true 的可调用对象,当应应用验证规则时。
返回
$this
描述
向字段添加信用卡规则。
语法
Email(string $field , boolean $checkMXfalse, string|null $messagenull, string|callable|null, $whennull)
参数 要应用规则的字段。 是否检查 MX 记录。 规则失败时的错误消息。 'create' 或 'update' 或返回 true 的可调用对象,当应应用验证规则时。
返回
$this
描述
向字段添加电子邮件验证规则。
语法
maxLength(string $field, integer $max, string|null $messagenull, string|callable|null $whennull)
参数 要应用规则的字段。 允许的最大长度。 规则失败时的错误消息。 'create''update'callable 在应应用验证规则时返回 true。
返回
$this
描述
向字段添加字符串长度验证规则。
语法
minLength(string $field, integer $min, string|null $messagenull, string|callable|null $whennull)
参数 要应用规则的字段。 允许的最大长度。 规则失败时的错误消息。 'create''update'callable,在应应用验证规则时返回 true。
返回
$this
描述
向字段添加字符串长度验证规则。
语法
notBlank(string $field, string|null $messagenull, string|callable|null $whennull)
参数 要应用规则的字段。 规则失败时的错误消息。 'create''update'callable 在应应用验证规则时返回 true。
返回
$this
描述
向字段添加 notBlank 规则。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4