企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
# 可用验证规则 以下是现有可用的验证规则清单与他们的函数名称: - - - - - - - Accepted - Active URL - After (Date) - Alpha - Alpha Dash - Alpha Numeric - Array - Before (Date) - Between - Boolean - Confirmed - Date - Date Format - Different - Digits - Digits Between - E-Mail - Exists (Database) - Image (File) - In - Integer - IP Address - Max - MIME Types - Min - Not In - Numeric - Regular Expression - Required - Required If - Required With - Required With All - Required Without - Required Without All - Same - Size - String - Timezone - Unique (Database) - URL ## accepted 字段值为 yes, on, 或是 1 时,验证才会通过。这在确认"服务条款"是否同意时很有用。 ## active\_url 字段值通过 PHP 函数 checkdnsrr 来验证是否为一个有效的网址。 ## after:date 验证字段是否是在指定日期之后。这个日期将会使用 PHP strtotime 函数验证。 ## alpha 字段仅全数为字母字串时通过验证。 ## alpha\_dash 字段值仅允许字母、数字、破折号(-)以及底线(\_) ## alpha\_num 字段值仅允许字母、数字 ## array 字段值仅允许为数组 ## before:date 验证字段是否是在指定日期之前。这个日期将会使用 PHP strtotime 函数验证。 ## between:min,max 字段值需介于指定的 min 和 max 值之间。字串、数值或是文件都是用同样的方式来进行验证。 ## confirmed 字段值需与对应的字段值 foo\_confirmation 相同。例如,如果验证的字段是 password ,那对应的字段 password\_confirmation 就必须存在且与 password 字段相符。 ## date 字段值通过 PHP strtotime 函数验证是否为一个合法的日期。 ## date\_format:format 字段值通过 PHP date\_parse\_from\_format 函数验证符合 format 制定格式的日期是否为合法日期。 ## different:field 字段值需与指定的字段 field 值不同。 ## digits:value 字段值需为数字且长度需为 value。 ## digits\_between:min,max 字段值需为数字,且长度需介于 min 与 max 之间。 ## boolean 字段必须可以转换成布尔值,可接受的值为 true, false, 1, 0, "1", "0"。 ## email 字段值需符合 email 格式。 ## exists:table,column 字段值需与存在于数据库 table 中的 column 字段值其一相同。 Exists 规则的基本使用方法 ``` 'state' => 'exists:states' ``` 指定一个自定义的字段名称 ``` 'state' => 'exists:states,abbreviation' ``` 您可以指定更多条件且那些条件将会被新增至 "where" 查询里: ``` 'email' => 'exists:staff,email,account_id,1' /* 这个验证规则为 email 需存在于 staff 这个数据库表中 email 字段中且 account_id=1 */ ``` 通过NULL搭配"where"的缩写写法去检查数据库的是否为NULL ``` 'email' => 'exists:staff,email,deleted_at,NULL' ``` ## image 文件必需为图片(jpeg, png, bmp, gif 或 svg) ## in:foo,bar,... 字段值需符合事先给予的清单的其中一个值 ## integer 字段值需为一个整数值 ## ip 字段值需符合 IP 位址格式。 ## max:value 字段值需小于等于 value。字串、数字和文件则是判断 size 大小。 ## mimes:foo,bar,... 文件的 MIME 类需在给定清单中的列表中才能通过验证。 MIME规则基本用法 ``` 'photo' => 'mimes:jpeg,bmp,png' ``` ## min:value 字段值需大于等于 value。字串、数字和文件则是判断 size 大小。 ## not\_in:foo,bar,... 字段值不得为给定清单中其一。 ## numeric 字段值需为数字。 ## regex:pattern 字段值需符合给定的正规表示式。 > 注意: 当使用regex模式时,您必须使用数组来取代"|"作为分隔,尤其是当正规表示式中含有"|"字串。 ## required 字段值为必填。 ## required\_if:field,value 字段值在 field 字段值为 value 时为必填。 ## required\_with:foo,bar,... 字段值 仅在 任一指定字段有值情况下为必填。 ## required\_with\_all:foo,bar,... 字段值 仅在 所有指定字段皆有值情况下为必填。 ## required\_without:foo,bar,... 字段值 仅在 任一指定字段没有值情况下为必填。 ## required\_without\_all:foo,bar,... 字段值 仅在 所有指定字段皆没有值情况下为必填。 ## same:field 字段值需与指定字段 field 等值。 ## size:value 字段值的尺寸需符合给定 value 值。对于字串来说,value 为需符合的字串长度。对于数字来说,value 为需符合的整数值。对于文件来说,value 为需符合的文件大小(单位 kb)。 ## timezone 字段值通过 PHP timezone\_identifiers\_list 函数来验证是否为有效的时区。 ## unique:table,column,except,idColumn 字段值在给定的数据库中需为唯一值。如果 column(字段) 选项没有指定,将会使用字段名称。 Occasionally, you may need to set a custom connection for database queries made by the Validator. As seen above, setting unique:users as a validation rule will use the default database connection to query the database. To override this, do the following: ``` $verifier = App::make('validation.presence'); $verifier->setConnection('connectionName'); $validator = Validator::make($input, [ 'name' => 'required', 'password' => 'required|min:8', 'email' => 'required|email|unique:users', ]); $validator->setPresenceVerifier($verifier); ``` 唯一(Unique)规则的基本用法 ``` 'email' => 'unique:users' ``` 指定一个自定义的字段名称 ``` 'email' => 'unique:users,email_address' ``` 强制唯一规则忽略指定的 ID ``` 'email' => 'unique:users,email_address,10' ``` 增加额外的 Where 条件 您也可以指定更多的条件式到 "where" 查询语句中: ``` 'email' => 'unique:users,email_address,NULL,id,account_id,1' ``` 上述规则为只有 account\_id 为 1 的数据列会做唯一规则的验证。 ## url 字段值需符合 URL 的格式。 > 注意: 此函数会使用 PHP filter\_var 方法验证。