Laravel框架表单验证详解(3)
'photo' => 'mimes:jpeg,bmp,png'
min:value
验证此规则的值必须大于最小值 value。字符串、数字以及文件都将使用大小规则进行比较。
not_in:foo,bar,...
验证此规则的值必须在给定的列表中不存在。
numeric
验证此规则的值必须是一个数字。
regex:pattern
验证此规则的值必须符合给定的正则表达式。
注意: 当使用 regex 模式的时候,有必要使用数组指定规则,而不是管道分隔符,特别是正则表达式中包含一个管道字符的时候。
required
验证此规则的值必须在输入数据中存在。
required_if:field,value
当指定的域为某个值的时候,验证此规则的值必须存在。
required_with:foo,bar,...
仅当指定的域存在的时候,验证此规则的值必须存在。
required_without:foo,bar,...
仅当指定的域不存在的时候,验证此规则的值必须存在。
same:field
验证此规则的值必须与给定域的值相同。
size:value
验证此规则的值的大小必须与给定的 value 相同。对于字符串,value 代表字符的个数;对于数字,value 代表它的整数值,对于文件,value 代表文件以KB为单位的大小。
unique:table,column,except,idColumn
验证此规则的值必须在给定的数据库的表中唯一。如果 column 没有被指定,将使用该域的名字。
Unique 规则的基础使用
'email' => 'unique:users'
指定列名
'email' => 'unique:users,email_address'
强制忽略一个给定的 ID
'email' => 'unique:users,email_address,10'
url
验证此规则的值必须是一个合法的 URL。
定制错误消息
如果有需要,您可以使用定制的错误消息代替默认的消息。这里有好几种定制错误消息的方法。
传递定制消息到验证器
$messages = array(
'required' => 'The :attribute field is required.',
);
$validator = Validator::make($input, $rules, $messages);
注意: :attribute 占位符将被实际的进行验证的域的名字代替,您也可以在错误消息中使用其他占位符。
其他验证占位符
$messages = array(
'same' => 'The :attribute and :other must match.',
'size' => 'The :attribute must be exactly :size.',
'between' => 'The :attribute must be between :min - :max.',
'in' => 'The :attribute must be one of the following types:
:values',
);
有些时候,您可能希望只对一个指定的域指定定制的错误消息:
对一个指定的域指定定制的错误消息
$messages = array(
'email.required' => 'We need to know your e-mail address!',
);
在一些情况下,您可能希望在一个语言文件中指定错误消息而不是直接传递给 Validator。为了实现这个目的,请在 app/lang/xx/validation.php 文件中添加您的定制消息到 custom 数组。
在语言文件中指定错误消息