Laravel 提供了丰富的验证规则,涵盖常见的数据验证需求。以下是常用的 Laravel 验证规则及其对应的示例,帮助你更好地理解和使用这些规则。
基本验证规则
| 规则 |
描述 |
示例 |
required |
必须存在字段。 |
'name' => 'required' |
nullable |
字段可以为空,但如果存在必须通过其他验证 |
'name' => 'nullable' |
filled |
字段非空时才进行验证 |
'name' => 'filled' |
present |
字段必须存在,但可以为空 |
'name' => 'present' |
sometimes |
有时需要验证(配合 Validator 使用) |
'name' => 'sometimes' |
字符串和数值规则
| 规则 |
描述 |
示例 |
string |
必须是字符串 |
'name' => 'string' |
integer |
必须是整数 |
'age' => 'integer' |
numeric |
必须是数值 |
'price' => 'numeric' |
boolean |
必须是布尔值 |
'active' => 'boolean' |
array |
必须是数组 |
'tags' => 'array' |
json |
必须是 JSON 字符串 |
'settings' => 'json' |
digits |
必须是指定长度的数字 |
'pin' => 'digits:4' |
digits_between |
必须是指定范围内的数字长度 |
'phone' => 'digits_between:10,15' |
min |
最小值(数字、字符串、文件或数组) |
'age' => 'min:18' |
max |
最大值(数字、字符串、文件或数组) |
'title' => 'max:255' |
日期和时间规则
| 规则 |
描述 |
示例 |
date |
必须是有效日期 |
'published_at' => 'date' |
date_format |
必须符合指定格式 |
'date' => 'date_format:Y-m-d' |
before |
必须是指定日期之前 |
'start_date' => 'before:end_date' |
after |
必须是指定日期之后 |
'end_date' => 'after:start_date' |
before_or_equal |
必须是指定日期或之前 |
'start_date' => 'before_or_equal:today' |
after_or_equal |
必须是指定日期或之后 |
'end_date' => 'after_or_equal:start_date' |
文件和图片规则
| 规则 |
描述 |
示例 |
file |
必须是文件 |
'document' => 'file' |
image |
必须是图片文件(jpeg、png 等) |
'avatar' => 'image' |
mimes |
文件 MIME 类型必须是指定类型 |
'avatar' => 'mimes:jpeg,png' |
mimetypes |
文件 MIME 类型必须是指定 MIME |
'avatar' => 'mimetypes:image/jpeg' |
size |
文件大小(单位:KB) |
'document' => 'size:1024' |
max |
文件最大大小 |
'document' => 'max:2048' |
dimensions |
图片的最小/最大宽度、高度 |
'avatar' => 'dimensions:min_width=100,min_height=200' |
字符串和格式规则
| 规则 |
描述 |
示例 |
email |
必须是有效的邮箱地址 |
'email' => 'email' |
url |
必须是有效的 URL |
'website' => 'url' |
ip |
必须是有效的 IP 地址 |
'ip_address' => 'ip' |
uuid |
必须是有效的 UUID |
'id' => 'uuid' |
regex |
必须符合正则表达式 |
'username' => 'regex:/^[A-Za-z0-9]+$/' |
alpha |
只能包含字母 |
'name' => 'alpha' |
alpha_dash |
只能包含字母、数字、破折号和下划线 |
'username' => 'alpha_dash' |
alpha_num |
只能包含字母和数字 |
'username' => 'alpha_num' |
比较规则
| 规则 |
描述 |
示例 |
same |
必须与指定字段的值相同 |
'password_confirmation' => 'same:password' |
different |
必须与指定字段的值不同 |
'old_password' => 'different:new_password' |
gt |
必须大于指定字段的值 |
'age' => 'gt:18' |
gte |
必须大于或等于指定字段的值 |
'quantity' => 'gte:min_quantity' |
lt |
必须小于指定字段的值 |
'age' => 'lt:60' |
lte |
必须小于或等于指定字段的值 |
'quantity' => 'lte:max_quantity' |
in |
值必须在指定的列表中 |
'status' => 'in:pending,approved' |
not_in |
值必须不在指定的列表中 |
'status' => 'not_in:banned' |
数组和 JSON 规则
| 规则 |
描述 |
示例 |
array |
必须是一个数组 |
'tags' => 'array' |
distinct |
数组的每个元素必须唯一 |
'tags.*' => 'distinct' |
size |
数组必须包含指定数量的元素 |
'tags' => 'size:3' |
min |
数组元素的最小数量 |
'tags' => 'min:1' |
max |
数组元素的最大数量 |
'tags' => 'max:5' |
特定字段条件验证
| 规则 |
描述 |
示例 |
required_if |
如果指定字段为某值时,字段是必填 |
'reason' => 'required_if:status,rejected' |
required_unless |
如果指定字段不为某值时,字段是必填 |
'reason' => 'required_unless:status,approved' |
required_with |
如果任一指定字段存在,则字段是必填 |
'field' => 'required_with:field1,field2' |
required_with_all |
如果所有指定字段存在,则字段是必填 |
'field' => 'required_with_all:field1,field2' |
required_without |
如果任一指定字段不存在,则字段是必填 |
'field' => 'required_without:field1' |
required_without_all |
如果所有指定字段都不存在,则字段是必填 |
'field' => 'required_without_all:field1,field2' |
数据库验证规则
| 规则 |
描述 |
示例 |
unique |
必须在数据库中唯一 |
'email' => 'unique:users,email' |
exists |
必须存在于数据库的指定表字段中 |
'user_id' => 'exists:users,id' |
自定义错误消息
你可以在验证中添加自定义错误消息,以便更好地提示用户:
1 2 3 4 5 6 7
| public function messages() { return [ 'name.required' => '请提供您的姓名。', 'email.unique' => '该邮箱地址已被注册。', ]; }
|
案例示例
以下是一个表单请求验证的综合示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserRequest extends FormRequest { public function rules() { return [ 'name' => 'required|string|max:255', '
email' => 'required|email|unique:users,email', 'password' => 'required|string|min:8|confirmed', 'age' => 'nullable|integer|gt:0', 'birth_date' => 'nullable|date|before:today', 'profile_image' => 'nullable|image|mimes:jpeg,png|max:1024', ]; }
public function messages() { return [ 'name.required' => '姓名为必填项。', 'email.unique' => '该邮箱已被注册。', 'password.confirmed' => '两次密码输入不一致。', 'birth_date.before' => '出生日期必须是过去的日期。', ]; } }
|
通过灵活使用这些规则,你可以更好地掌控数据的验证逻辑,提升应用的安全性和用户体验。