laravel 里面Validator的使用案例
-
$validator = Validator::make($request->all(), [
-
'nickname' => 'required|max:8|string',
-
'signature' => 'string|max:30',
-
'gender' => ' integer',
-
'height' => ' digits_between:50,300',
-
'weight' => ' integer|min:2|max:3',
-
'birth_date' => 'date',
-
'city' => 'string',
-
'order_number' => 'exists:orders,order_number',
-
'openid' => 'exists:orders,openid',
-
'activity_id' => 'required'
-
]);
-
if ($validator->fails()) {
-
return $this->responseForJson(ERR_ACCESS_DEN, $validator->errors());
-
}
上面是使用简单案例;下面详细列出来可用规则
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 格式。
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 方法验证
使用 Laravel 的 ValidatesRequests
trait 验证请求是非常简单的,该 trait 已经通过 BaseController
被自动包含进来。它的功能非常强大,并为常用的情况提供了多个有用的规则。其中 exists()
和 unique()
用于验证数据是否在数据库中存在,通常使用方式如下:
- // exists 例子
- 'email' => 'exists:staff,account_id,1'
- // unique 例子
- 'email' => 'unique:users,email_address,$user->id,id,account_id,1'
你可以看到,这种方式不便于记忆,有时候你可能经常需要去查看文档。
从 Laravel v5.3.18 开始,这两个规则都通过新引入的 Rule
类被简化了。
使用上面相同的例子,通过下面这个更流畅的例子可以达到相同的效果:
- 'email' => [
- 'required',
- Rule::exists('staff')->where(function ($query) {
- $query->where('account_id', 1);
- }),
- ],
- 'email' => [
- 'required',
- Rule::unique('users')->ignore($user->id)->where(function ($query) {
- $query->where('account_id', 1);
- })
- ],
这两个方法都支持下面的方法:
where
whereNot
whereNull
whereNotNull
unique
方法包含额外的 ignore
方法,所以你可以验证其他的数据。
这个新特性的另一个福利是旧的方式依然完全支持,实现方式是通过 formatWheres 方法转换为旧的字符串方式:
- protected function formatWheres()
- {
- return collect($this->wheres)->map(function ($where) {
- return $where['column'].','.$where['value'];
- })->implode(',');
- }
要获取最新版本你需要运行 composer update
,一旦获取到 v5.3.18 你就可以使用这种全新的流畅的方式了。
Update On 2016-10-16:
demisions 规则也将在 Laravel v5.3.19 中加入新的方式:
via: laravel-news