Laravel 验证规则列表

Posted

技术标签:

【中文标题】Laravel 验证规则列表【英文标题】:List of Laravel validation rules 【发布时间】:2018-04-23 00:40:06 【问题描述】:

是否有 Laravel 请求验证的所有可用规则的​​主列表?我只看过文档中显示的那些,但肯定不止 4-5 条规则。我知道我可以制作自定义的,我目前正在这样做,但我想知道所有可用的。

【问题讨论】:

您在查看文档的哪个位置? Here is a list of available rules that is much longer than 4-5 rules @jfadich 我显然看起来不正确。我翻遍了整个页面,但找不到。谢谢!如果你能把它写成答案,我会把它标记为正确的。 【参考方案1】:

如果您检查 Illuminate\Validation\Validator 类,它的顶部有相当多的数组,具有不同的内置验证规则。

【讨论】:

【参考方案2】:

验证规则远不止 4-5 条。来自docs。

这是当前列表(Laravel 8):

Accepted

Active URL

After (Date)

After Or Equal (Date)

Alpha

Alpha Dash

Alpha Numeric

Array

Bail

Before (Date)

Before Or Equal (Date)

Between

Boolean

Confirmed

Current Password

Date

Date Equals

Date Format

Different

Digits

Digits Between

Dimensions (Image Files)

Distinct

Email

Ends With

Exclude If

Exclude Unless

Exists (Database)

File

Filled

Greater Than

Greater Than Or Equal

Image (File)

In

In Array

Integer

IP Address

JSON

Less Than

Less Than Or Equal

Max

MIME Types

MIME Type By File Extension

Min

Multiple Of

Not In

Not Regex

Nullable

Numeric

Password

Present

Prohibited

Prohibited If

Prohibited Unless

Regular Expression

Required

Required If

Required Unless

Required With

Required With All

Required Without

Required Without All

Same

Size

Sometimes

Starts With

String

Timezone

Unique (Database)

URL

UUID

【讨论】:

【参考方案3】:

Laravel 文档列出了所有的验证器

https://laravel.com/docs/5.5/validation

【讨论】:

【参考方案4】:

我不知道其他人是否会觉得这很有用,但您可以使用反射来找出验证器:

$validatorClass = new ReflectionClass(Illuminate\Validation\Concerns\ValidatesAttributes::class);

collect($validatorClass->getMethods(ReflectionMethod::IS_PUBLIC))
  ->filter(function($method)
    return Illuminate\Support\Str::startsWith($method->name,'validate');
  )
  ->map(function($method)
    return str_replace('validate_', '', Illuminate\Support\Str::snake($method->name));
  )
  ->dd();

array:68 [
  0 => "accepted"
  1 => "active_url"
  2 => "bail"
  3 => "before"
  4 => "before_or_equal"
  5 => "after"
  6 => "after_or_equal"
  7 => "alpha"
  8 => "alpha_dash"
  9 => "alpha_num"
  10 => "array"
  11 => "between"
  12 => "boolean"
  13 => "confirmed"
  14 => "date"
  15 => "date_format"
  16 => "date_equals"
  17 => "different"
  18 => "digits"
  19 => "digits_between"
  20 => "dimensions"
  21 => "distinct"
  22 => "email"
  23 => "exists"
  24 => "unique"
  28 => "file"
  29 => "filled"
  30 => "gt"
  31 => "lt"
  32 => "gte"
  33 => "lte"
  34 => "image"
  35 => "in"
  36 => "in_array"
  37 => "integer"
  38 => "ip"
  39 => "ipv4"
  40 => "ipv6"
  41 => "json"
  42 => "max"
  43 => "mimes"
  44 => "mimetypes"
  45 => "min"
  46 => "nullable"
  47 => "not_in"
  48 => "numeric"
  49 => "present"
  50 => "regex"
  51 => "not_regex"
  52 => "required"
  53 => "required_if"
  54 => "exclude_if"
  55 => "exclude_unless"
  56 => "exclude_without"
  57 => "required_unless"
  58 => "required_with"
  59 => "required_with_all"
  60 => "required_without"
  61 => "required_without_all"
  62 => "same"
  63 => "size"
  64 => "sometimes"
  65 => "starts_with"
  66 => "ends_with"
  67 => "string"
  68 => "timezone"
  69 => "url"
  70 => "uuid"
]

【讨论】:

以上是关于Laravel 验证规则列表的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 有时验证规则

Laravel:模型内的验证。多重验证规则

laravel验证规则

Laravel 验证规则“不同”

Laravel 验证唯一规则反射类异常

如何覆盖验证规则登录 Laravel\Fortify?