<article><h1>Laravel 的表单验证机制详解</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#validation-quickstart">快速上手</a><ul><li><a href="#quick-defining-the-routes">定义路由</a></li><li><a href="#quick-creating-the-controller">创建控制器</a></li><li><a href="#quick-writing-the-validation-logic">编写验证逻辑</a></li><li><a href="#quick-displaying-the-validation-errors">显示验证错误</a></li><li><a href="#a-note-on-optional-fields">有关可选字段的注意事项</a></li></ul></li><li><a href="#form-request-validation">表单请求验证</a><ul><li><a href="#creating-form-requests">创建表单请求</a></li><li><a href="#authorizing-form-requests">授权表单请求</a></li><li><a href="#customizing-the-error-format">自定义错误格式</a></li><li><a href="#customizing-the-error-messages">自定义错误消息</a></li></ul></li><li><a href="#manually-creating-validators">手动创建表单验证</a><ul><li><a href="#automatic-redirection">自动重定向</a></li><li><a href="#named-error-bags">命名错误包</a></li><li><a href="#after-validation-hook">验证后钩子</a></li></ul></li><li><a href="#working-with-error-messages">处理错误消息</a><ul><li><a href="#custom-error-messages">自定义错误消息</a></li></ul></li><li><a href="#available-validation-rules">可用的验证规则</a></li><li><a href="#conditionally-adding-rules">按条件增加规则</a></li><li><a href="#validating-arrays">验证数组</a></li><li><a href="#custom-validation-rules">自定义验证规则</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">Introduction</a></h2><p>Laravel 提供了多种不同的验证方法来对应用程序传入的数据进行验证。默认情况下,Laravel 的基类控制器使用 <code class=" language-php">ValidatesRequests</code> Trait,它提供了方便的方法使用各种强大的验证规则来验证传入的 HTTP 请求数据。</p><p><a name="validation-quickstart"></a></p><h2><a href="#validation-quickstart">快速上手</a></h2><p>为了了解 Laravel 强大验证特性,我们先来看看一个完整的表单验证并返回错误消息的示例。</p><p><a name="quick-defining-the-routes"></a></p><h3>定义路由</h3><p>首先,我们假定在 <code class=" language-php">routes<span class="token operator">/</span>web<span class="token punctuation">.</span>php</code> 文件中定义了以下路由:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'post/create'</span><span class="token punctuation">,</span> <span class="token string">'PostController@create'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">post<span class="token punctuation">(</span></span><span class="token string">'post'</span><span class="token punctuation">,</span> <span class="token string">'PostController@store'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><code class=" language-php"><span class="token constant">GET</span></code> 路由会显示一个用于创建新博客文章的表单,<code class=" language-php"><span class="token constant">POST</span></code> 路由则会将新的博客文章保存到数据库。</p><p><a name="quick-creating-the-controller"></a></p><h3>创建控制器</h3><p>下一步,我们来看一个处理这些路由的简单的控制器。我们将 <code class=" language-php">store</code> 方法置空:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">PostController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 显示创建博客文章的表单。
*
* @return Response
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">create<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'post.create'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">/**
* 保存一个新的博客文章。
*
* @param Request $request
* @return Response
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">store<span class="token punctuation">(</span></span>Request <span class="token variable">$request</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> // 验证以及保存博客发表文章...
</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p><a name="quick-writing-the-validation-logic"></a></p><h3>编写验证逻辑</h3><p>现在我们准备开始编写 <code class=" language-php">store</code> 逻辑方法来验证我们博客发布的新文章。检查应用程序的基底控制器 (<code class=" language-php">App\<span class="token package">Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span></code>) 类你会看到这个类使用了 <code class=" language-php">ValidatesRequests</code> Trait。这个 Trait 在你所有的控制器里提供了方便的 <code class=" language-php">validate</code> 验证方法。</p><p><code class=" language-php">validate</code> 方法会接收 HTTP 传入的请求以及验证的规则。如果验证通过,你的代码就可以正常的运行。若验证失败,则会抛出异常错误消息并自动将其返回给用户。在一般的 HTTP 请求下,都会生成一个重定向响应,而对于 AJAX 请求则会发送 JSON 响应。</p><p>让我们接着回到 <code class=" language-php">store</code> 方法来深入理解 <code class=" language-php">validate</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 保存一篇新的博客文章。
*
* @param Request $request
* @return Response
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">store<span class="token punctuation">(</span></span>Request <span class="token variable">$request</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">validate<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'title'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span>
<span class="token string">'body'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true"> // 文章内容是符合规则的,存入数据库
</span><span class="token punctuation">}</span></code></pre><p>如你所见,我们将本次 HTTP 请求及所需的验证规则传递至 <code class=" language-php">validate</code> 方法中。另外再提醒一次,如果验证失败,将会自动生成一个对应的响应。如果验证通过,那我们的控制器将会继续正常运行。</p><h4>在第一次验证失败后停止</h4><p>有时,你希望在某个属性第一次验证失败后停止运行验证规则。为了达到这个目的,附加 <code class=" language-php">bail</code> 规则到该属性:</p><pre class=" language-php"><code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">validate<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'title'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'bail|required|unique:posts|max:255'</span><span class="token punctuation">,</span>
<span class="token string">'body'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>在这个例子里,如果 title 字段 没有通过 <code class=" language-php">required</code> 的验证规则,那么 <code class=" language-php">unique</code> 这个规则将不会被检测了。将按规则被分配的顺序来验证规则。</p><h4>嵌套属性的注解</h4><p>如果你的 HTTP 请求包含一个 「嵌套的」 参数,你可以在验证规则中通过 「点」 语法来指定这些参数。</p><pre class=" language-php"><code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">validate<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'title'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span>
<span class="token string">'author.name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token string">'author.description'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="quick-displaying-the-validation-errors"></a></p><h3>显示验证错误</h3><p>如果本次请求的参数未通过我们指定的验证规则呢?正如前面所提到的,Laravel 会自动把用户重定向到先前的位置。另外,所有的验证错误会被自动 <a href="/docs/5.4/session#flash-data">闪存至 session</a>。</p><p>再者,请注意在 <code class=" language-php"><span class="token constant">GET</span></code> 路由中,我们无需显式的将错误信息和视图绑定起来。这是因为 Lavarel 会检查在 Session 数据中的错误信息,然后如果对应的视图存在的话,自动将它们绑定起来。变量 <code class=" language-php"><span class="token variable">$errors</span></code> 会成为 <code class=" language-php">Illuminate\<span class="token package">Support<span class="token punctuation">\</span>MessageBag</span></code> 的一个实例对象。要获取关于这个对象的更多信息,请<a href="#working-with-error-messages">查阅这个文档</a>。</p><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> <code class=" language-php"><span class="token variable">$errors</span></code> 变量被 <code class=" language-php">Illuminate\<span class="token package">View<span class="token punctuation">\</span>Middleware<span class="token punctuation">\</span>ShareErrorsFromSession</span></code> 中间件绑定到视图,该中间件由 <code class=" language-php">web</code> 中间件组提供。<strong>当这个中间件被应用后,在你的视图中就可以获取到 <code class=" language-php"><span class="token variable">$error</span></code> 变量</strong>,可以使你方便的假定 <code class=" language-php"><span class="token variable">$errors</span></code> 变量总是已经被定义好并且可以安全的使用。</p></blockquote><p>所以,在我们的例子中,当验证失败的时候,用户将会被重定向到 <code class=" language-php">create</code> 方法,让我们在视图中显示错误信息:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token comment" spellcheck="true"><!-- /resources/views/post/create.blade.php --></span></span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h1</span><span class="token punctuation">></span></span></span>创建文章<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h1</span><span class="token punctuation">></span></span></span>
@<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">count<span class="token punctuation">(</span></span><span class="token variable">$errors</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>alert alert-danger<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ul</span><span class="token punctuation">></span></span></span>
@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$error</span><span class="token punctuation">)</span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$error</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span></span>
@<span class="token keyword">endforeach</span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ul</span><span class="token punctuation">></span></span></span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span></span>
@<span class="token keyword">endif</span>
<span class="token markup"><span class="token comment" spellcheck="true"><!-- 创建文章表单 --></span></span></code></pre><p><a name="a-note-on-optional-fields"></a></p><h3>有关可选字段的注意事项</h3><p>默认情况下,Laravel 会在你的应用中的全局中间件栈中包含 <code class=" language-php">TrimStrings</code> 和 <code class=" language-php">ConvertEmptyStringsToNull</code> 中间件。这些中间件在 <code class=" language-php">App\<span class="token package">Http<span class="token punctuation">\</span>Kernel</span></code> 类中。因此,如果您不希望验证程序将「null」值视为无效的,您通常需要将「可选」的请求字段标记为 <code class=" language-php">nullable</code>。</p><pre class=" language-php"><code class=" language-php"><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">validate<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'title'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span>
<span class="token string">'body'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token string">'publish_at'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'nullable|date'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>在这个例子里,我们指定 <code class=" language-php">publish_at</code> 字段可以为 <code class=" language-php"><span class="token keyword">null</span></code> 或者一个有效的日期格式。如果 <code class=" language-php">nullable</code> 的修饰词没有添加到规则定义中,验证器会认为 <code class=" language-php"><span class="token keyword">null</span></code> 是一个无效的日期格式。</p><p><a name="quick-customizing-the-flashed-error-format"></a></p><h4>自定义闪存的错误消息格式</h4><p>当验证失败时,如果你想要在闪存上自定义验证的错误格式,则需在控制器中重写 <code class=" language-php">formatValidationErrors</code>。别忘了将 <code class=" language-php">Illuminate\<span class="token package">Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span></code> 类引入到文件上方:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>DispatchesJobs</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Routing<span class="token punctuation">\</span>Controller</span> <span class="token keyword">as</span> BaseController<span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidatesRequests</span><span class="token punctuation">;</span>
<span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">Controller</span> <span class="token keyword">extends</span> <span class="token class-name">BaseController</span>
<span class="token punctuation">{</span>
<span class="token keyword">use</span> <span class="token package">DispatchesJobs</span><span class="token punctuation">,</span> ValidatesRequests<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/**
* {@inheritdoc}
*/</span>
<span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function">formatValidationErrors<span class="token punctuation">(</span></span>Validator <span class="token variable">$validator</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$validator</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">errors<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p><a name="quick-ajax-requests-and-validation"></a></p><h4>AJAX 请求验证</h4><p>在这个例子中,我们使用一种传统的方式来将数据发送到应用程序上。当我们在 AJAX 的请求中使用 <code class=" language-php">validate</code> 方法时,Laravel 并不会生成一个重定向响应,而是会生成一个包含所有错误验证的 JSON 响应。这个 JSON 响应会发送一个 422 HTTP 状态码。</p><p><a name="form-request-validation"></a></p><h2><a href="#form-request-validation">表单请求验证</a></h2><p><a name="creating-form-requests"></a></p><h3>创建表单请求</h3><p>在更复杂的验证情境中,你可能会想要创建一个「表单请求( form request )」。表单请求是一个自定义的请求类,里面包含着验证逻辑。要创建一个表单请求类,可使用 Artisan 命令行命令 <code class=" language-php">make<span class="token punctuation">:</span>request</code> :</p><pre class=" language-php"><code class=" language-php">php artisan make<span class="token punctuation">:</span>request StoreBlogPost</code></pre><p>The generated class will be placed in the <code class=" language-php">app<span class="token operator">/</span>Http<span class="token operator">/</span>Requests</code> directory. If this directory does not exist, it will be created when you run the <code class=" language-php">make<span class="token punctuation">:</span>request</code> command. Let's add a few validation rules to the <code class=" language-php">rules</code> method:</p><p>新生成的类保存在 <code class=" language-php">app<span class="token operator">/</span>Http<span class="token operator">/</span>Requests</code> 目录下。如果这个目录不存在,那么将会在你运行 <code class=" language-php">make<span class="token punctuation">:</span>request</code> 命令时创建出来。让我们添加一些验证规则到 <code class=" language-php">rules</code> 方法中:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取适用于请求的验证规则。
*
* @return array
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">rules<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">[</span>
<span class="token string">'title'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span>
<span class="token string">'body'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>怎样才能较好的运行验证规则呢?你所需要做的就是在控制器方法中利用类型提示传入请求。传入的请求会在控制器方法被调用前进行验证,意思就是说你不会因为验证逻辑而把控制器弄得一团糟:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 保存传入的博客文章。ß
*
* @param StoreBlogPost $request
* @return Response
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">store<span class="token punctuation">(</span></span>StoreBlogPost <span class="token variable">$request</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> // The incoming request is valid...
</span><span class="token punctuation">}</span></code></pre><p>如果验证失败,就会生成一个重定向响应把用户返回到先前的位置。这些错误会被闪存到 Session,所以这些错误都可以被显示。如果进来的是 AJAX 请求的话,则会传回一个 HTTP 响应,其中包含了 422 状态码和验证错误的 JSON 数据。</p><h4>添加表单请求后钩子</h4><p>如果你想在表单请求「之后」添加钩子,你可以使用 <code class=" language-php">withValidator</code> 方法。这个方法接收一个完整的验证类,允许你在实际判断验证规则调之前调用验证类的所有方法:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* @param \Illuminate\Validation\Validator $validator
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">withValidator<span class="token punctuation">(</span></span><span class="token variable">$validator</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$validator</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">after<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">somethingElseIsInvalid<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$validator</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">errors<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">add<span class="token punctuation">(</span></span><span class="token string">'field'</span><span class="token punctuation">,</span> <span class="token string">'Something is wrong with this field!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="authorizing-form-requests"></a></p><h3>授权表单请求</h3><p>表单的请求类内包含了 <code class=" language-php">authorize</code> 方法。在这个方法中,你可以确认用户是否真的通过了授权,以便更新指定数据。比方说,有一个用户想试图去更新一篇文章的评论,你能保证他确实是这篇评论的拥有者吗?具体代码如下:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 判断用户是否有权限做出此请求。
*
* @return bool
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">authorize<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$comment</span> <span class="token operator">=</span> <span class="token scope">Comment<span class="token punctuation">::</span></span><span class="token function">find<span class="token punctuation">(</span></span><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">route<span class="token punctuation">(</span></span><span class="token string">'comment'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token variable">$comment</span> <span class="token operator">&&</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">user<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">can<span class="token punctuation">(</span></span><span class="token string">'update'</span><span class="token punctuation">,</span> <span class="token variable">$comment</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>由于所有的表单请求都是扩展于基础的 Laravel 请求类,所以我们可以使用 <code class=" language-php">user</code> 方法去获取当前认证登录的用户。同时请注意上述例子中对 <code class=" language-php">route 方法的调用。这个方法授权你获取调用的路由规则中的 <span class="token constant">URI</span> 参数,譬如下面例子中的</code>{comment}`参数:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">post<span class="token punctuation">(</span></span><span class="token string">'comment/{comment}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果 <code class=" language-php">authorize</code> 方法返回 <code class=" language-php"><span class="token boolean">false</span></code>,则会自动返回一个 HTTP 响应,其中包含 403 状态码,而你的控制器方法也将不会被运行。</p><p>如果你打算在应用程序的其它部分处理授权逻辑,只需从 <code class=" language-php">authorize</code> 方法返回 <code class=" language-php"><span class="token boolean">true</span></code> :</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 判断用户是否有权限做出此请求。
*
* @return bool
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">authorize<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="customizing-the-error-format"></a></p><h3>自定义错误格式</h3><p>如果你想要自定义验证失败时闪存到 Session 的验证错误格式,可在你的基底请求 (App\Http\Requests\Request) 中重写 <code class=" language-php">formatErrors</code>。别忘了文件上方引入 <code class=" language-php">Illuminate\<span class="token package">Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span></code> 类:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* {@inheritdoc}
*/</span>
<span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function">formatErrors<span class="token punctuation">(</span></span>Validator <span class="token variable">$validator</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$validator</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">errors<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="customizing-the-error-messages"></a></p><h3>自定义错误消息</h3><p>你可以通过重写表单请求的 <code class=" language-php">messages</code> 方法来自定义错误消息。此方法必须返回一个数组,其中含有成对的属性或规则以及对应的错误消息:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 获取已定义验证规则的错误消息。
*
* @return array
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">messages<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">[</span>
<span class="token string">'title.required'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'A title is required'</span><span class="token punctuation">,</span>
<span class="token string">'body.required'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'A message is required'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p><a name="manually-creating-validators"></a></p><h2><a href="#manually-creating-validators">手动创建验证请求</a></h2><p>如果你不想要使用 <code class=" language-php">ValidatesRequests</code> Trait 的 <code class=" language-php">validate</code> 方法,你可以手动创建一个 validator 实例并通过 <code class=" language-php"><span class="token scope">Validator<span class="token punctuation">::</span></span>make</code> 方法在 <a href="/docs/5.4/facades">Facade</a> 生成一个新的 <code class=" language-php">validator</code> 实例:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Validator</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">PostController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 保存一篇新的博客文章。
*
* @param Request $request
* @return Response
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">store<span class="token punctuation">(</span></span>Request <span class="token variable">$request</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'title'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span>
<span class="token string">'body'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">fails<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'post/create'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">withErrors<span class="token punctuation">(</span></span><span class="token variable">$validator</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">withInput<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true"> // 保存文章
</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>第一个传给 <code class=" language-php">make</code> 方法的参数是验证数据。第二个参数则是数据的验证规则。</p><p>如果请求没有通过验证,则可以使用 <code class=" language-php">withErrors</code> 方法把错误消息闪存到 Session。在进行重定向之后,<code class=" language-php"><span class="token variable">$errors</span></code> 变量可以在视图中自动共用,让你可以轻松地显示这些消息并返回给用户。<code class=" language-php">withErrors</code> 方法接收 validator、MessageBag,或 PHP array。</p><p><a name="automatic-redirection"></a></p><h3>自动重定向</h3><p>如果你想手动创建一个验证器实例,但希望继续享用 <code class=" language-php">ValidatesRequest</code> 特性提供的自动跳转功能,那么你可以调用一个现存的验证器实例中的 <code class=" language-php">validate</code> 方法。如果验证失败了,用户会被自动重定向,或者在 AJAX 请求中,一个 JSON 格式的响应将会被返回:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'title'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span>
<span class="token string">'body'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">validate<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="named-error-bags"></a></p><h3>命名错误包</h3><p>如果你在一个一个页面中有多个表单,你也许会希望命名错误信息包 <code class=" language-php">MessageBag</code> ,错误信息包允许你从指定的表单中接收错误信息。简单的给 <code class=" language-php">withErrors</code> 方法传递第二个参数作为一个名字:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'register'</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">withErrors<span class="token punctuation">(</span></span><span class="token variable">$validator</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>然后你能从 <code class=" language-php"><span class="token variable">$errors</span></code> 变量中获取到 <code class=" language-php">MessageBag</code> 实例:</p><pre class=" language-php"><code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$errors</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">login</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">first<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p><a name="after-validation-hook"></a></p><h3>验证后钩子</h3><p>验证器允许你在验证完成之后附加回调函数。这使得你可以容易的执行进一步验证,甚至可以在消息集合中添加更多的错误信息。使用它只需在验证实例中使用 <code class=" language-php">after</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$validator</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">after<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">somethingElseIsInvalid<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$validator</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">errors<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">add<span class="token punctuation">(</span></span><span class="token string">'field'</span><span class="token punctuation">,</span> <span class="token string">'Something is wrong with this field!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">fails<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><p><a name="working-with-error-messages"></a></p><h2><a href="#working-with-error-messages">处理错误消息</a></h2><p>调用 <code class=" language-php">Validator</code> 实例的 <code class=" language-php">errors</code> 方法,会得到一个 <code class=" language-php">Illuminate\<span class="token package">Support<span class="token punctuation">\</span>MessageBag</span></code> 的实例,里面有许多可让你操作错误消息的便利方法。<code class=" language-php"><span class="token variable">$errors</span></code> 值可以自动的被所有的视图获取,并且是一个MessageBag类的实例。自动对所有视图可用的 <code class=" language-php"><span class="token variable">$errors</span></code> 变量也是 <code class=" language-php">MessageBag</code> 类的一个实例。</p><h4>查看特定字段的第一个错误消息</h4><p>如果要查看特定字段的第一个错误消息,可以使用 <code class=" language-php">first</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$errors</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">errors<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token variable">$errors</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">first<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>查看特定字段的所有错误消息</h4><p>如果你想通过指定字段来简单的获取所有消息中的一个数组,则可以使用 <code class=" language-php">get</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><p>如果你正在验证的一个表单字段类型是数组,你可以使用 <code class=" language-php"><span class="token operator">*</span></code> 来获取每个元素的所有错误信息:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'attachments.*'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><h4>查看所有字段的所有错误消息</h4><p>如果你想要得到所有字段的消息数组,则可以使用 <code class=" language-php">all</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><h4>判断特定字段是否含有错误消息</h4><p>可以使用 <code class=" language-php">has</code> 方法来检测一个给定的字段是否存在错误信息:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">has<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span></code></pre><p><a name="custom-error-messages"></a></p><h3>自定义错误消息</h3><p>如果有需要的话,你也可以自定义错误的验证消息来取代默认的验证消息。有几种方法可以指定自定义消息。首先,你需要先通过传递三个参数到 <code class=" language-php"><span class="token scope">Validator<span class="token punctuation">::</span></span>make</code> 方法来自定义验证消息:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token string">'required'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'The :attribute field is required.'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">,</span> <span class="token variable">$messages</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>在这个例子中,<code class=" language-php"><span class="token punctuation">:</span>attribute</code> 占位符会被通过验证的字段实际名称所取代。除此之外,你还可以使用其它默认字段的验证提示消息。例如:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token string">'same'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'The :attribute and :other must match.'</span><span class="token punctuation">,</span>
<span class="token string">'size'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'The :attribute must be exactly :size.'</span><span class="token punctuation">,</span>
<span class="token string">'between'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'The :attribute must be between :min - :max.'</span><span class="token punctuation">,</span>
<span class="token string">'in'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'The :attribute must be one of the following types: :values'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span></code></pre><h4>指定自定义消息到特定的属性</h4><p>有时候你可能想要对特定的字段来自定义错误消息。只需在属性名称后加上「.」符号和指定验证的规则即可:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token string">'email.required'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'We need to know your e-mail address!'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span></code></pre><p><a name="localization"></a></p><h4>在语言文件中指定自定义的消息提示</h4><p>多数情况下,你会在语言文件中指定自定义的消息提示,而不是将定制的消息传递给 <code class=" language-php">Validator</code> 。实现它需要在语言文件 <code class=" language-php">resources<span class="token operator">/</span>lang<span class="token operator">/</span>xx<span class="token operator">/</span>validation<span class="token punctuation">.</span>php</code> 中,将定制的消息添加到 <code class=" language-php">custom</code> 数组。</p><pre class=" language-php"><code class=" language-php"><span class="token string">'custom'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'required'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'We need to know your e-mail address!'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><h4>在语言文件中自定义属性</h4><p>如果希望将验证消息的<code class=" language-php"><span class="token punctuation">:</span>attribute</code> 部分替换为自定义属性名称,则可以在 <code class=" language-php">resources<span class="token operator">/</span>lang<span class="token operator">/</span>xx<span class="token operator">/</span>validation<span class="token punctuation">.</span>php</code> 语言文件的 <code class=" language-php">attributes</code> 数组中指定自定义名称:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'attributes'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'email address'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p><a name="available-validation-rules"></a></p><h2><a href="#available-validation-rules">可用的验证规则</a></h2><p>以下是所有可用的验证规则清单与功能:</p><style>.collection-method-list > p {
column-count: 3; -moz-column-count: 3; -webkit-column-count: 3;
column-gap: 2em; -moz-column-gap: 2em; -webkit-column-gap: 2em;
}
.collection-method-list a {
display: block;
}</style><div class="collection-method-list"><p><a href="#rule-accepted">Accepted</a> <a href="#rule-active-url">Active URL</a> <a href="#rule-after">After (Date)</a> <a href="#rule-after-or-equal">After Or Equal (Date)</a> <a href="#rule-alpha">Alpha</a> <a href="#rule-alpha-dash">Alpha Dash</a> <a href="#rule-alpha-num">Alpha Numeric</a> <a href="#rule-array">Array</a> <a href="#rule-before">Before (Date)</a> <a href="#rule-before-or-equal">Before Or Equal (Date)</a> <a href="#rule-between">Between</a> <a href="#rule-boolean">Boolean</a> <a href="#rule-confirmed">Confirmed</a> <a href="#rule-date">Date</a> <a href="#rule-date-format">Date Format</a> <a href="#rule-different">Different</a> <a href="#rule-digits">Digits</a> <a href="#rule-digits-between">Digits Between</a> <a href="#rule-dimensions">Dimensions (Image Files)</a> <a href="#rule-distinct">Distinct</a> <a href="#rule-email">E-Mail</a> <a href="#rule-exists">Exists (Database)</a> <a href="#rule-file">File</a> <a href="#rule-filled">Filled</a> <a href="#rule-image">Image (File)</a> <a href="#rule-in">In</a> <a href="#rule-in-array">In Array</a> <a href="#rule-integer">Integer</a> <a href="#rule-ip">IP Address</a> <a href="#rule-json">JSON</a> <a href="#rule-max">Max</a> <a href="#rule-mimetypes">MIME Types</a> <a href="#rule-mimes">MIME Type By File Extension</a> <a href="#rule-min">Min</a> <a href="#rule-nullable">Nullable</a> <a href="#rule-not-in">Not In</a> <a href="#rule-numeric">Numeric</a> <a href="#rule-present">Present</a> <a href="#rule-regex">Regular Expression</a> <a href="#rule-required">Required</a> <a href="#rule-required-if">Required If</a> <a href="#rule-required-unless">Required Unless</a> <a href="#rule-required-with">Required With</a> <a href="#rule-required-with-all">Required With All</a> <a href="#rule-required-without">Required Without</a> <a href="#rule-required-without-all">Required Without All</a> <a href="#rule-same">Same</a> <a href="#rule-size">Size</a> <a href="#rule-string">String</a> <a href="#rule-timezone">Timezone</a> <a href="#rule-unique">Unique (Database)</a> <a href="#rule-url">URL</a></p></div><p><a name="rule-accepted"></a></p><h4>accepted</h4><p>验证字段值是否为 <em>yes</em>、<em>on</em>、<em>1</em>、或 <em>true</em>。这在确认「服务条款」是否同意时相当有用。</p><p><a name="rule-active-url"></a></p><h4>active_url</h4><p>根据 PHP 函数 <code class=" language-php">dns_get_record</code>,判断要验证的字段必须具有有效的 A 或 AAAA 记录。</p><p><a name="rule-after"></a></p><h4>after:<em>date</em></h4><p>验证字段是否是在指定日期之后。这个日期将会通过 <code class=" language-php">strtotime</code> 函数来验证。</p><pre class=" language-php"><code class=" language-php"><span class="token string">'start_date'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required|date|after:tomorrow'</span></code></pre><p>作为替换 <code class=" language-php">strtotime</code> 传递的日期字符串,你可以指定其它的字段来比较日期:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'finish_date'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required|date|after:start_date'</span></code></pre><p><a name="rule-after-or-equal"></a></p><h4>after_or_equal:<em>date</em></h4><p>验证字段必需是等于指定日期或在指定日期之后。更多信息请参见 <a href="#rule-after">after</a> 规则。</p><p><a name="rule-alpha"></a></p><h4>alpha</h4><p>验证字段值是否仅包含字母字符。</p><p><a name="rule-alpha-dash"></a></p><h4>alpha_dash</h4><p>验证字段值是否仅包含字母、数字、破折号( - )以及下划线( _ )。</p><p><a name="rule-alpha-num"></a></p><h4>alpha_num</h4><p>验证字段值是否仅包含字母、数字。</p><p><a name="rule-array"></a></p><h4>array</h4><p>验证字段必须是一个 PHP 数组。</p><p><a name="rule-before"></a></p><h4>before:<em>date</em></h4><p>验证字段是否是在指定日期之前。这个日期将会通过 <code class=" language-php">strtotime</code> 函数来验证。</p><p><a name="rule-before-or-equal"></a></p><h4>before_or_equal:<em>date</em></h4><p>验证字段是否是在指定日期之前。这个日期将会使用 PHP <code class=" language-php">strtotime</code> 函数来验证。</p><p><a name="rule-between"></a></p><h4>between:<em>min</em>,<em>max</em></h4><p>验证字段值的大小是否介于指定的 <em>min</em> 和 <em>max</em> 之间。字符串、数值或是文件大小的计算方式和 <a href="#rule-size"><code class=" language-php">size</code></a> 规则相同。</p><p><a name="rule-boolean"></a></p><h4>boolean</h4><p>验证字段值是否能够转换为布尔值。可接受的参数为 <code class=" language-php"><span class="token boolean">true</span></code>、<code class=" language-php"><span class="token boolean">false</span></code>、<code class=" language-php"><span class="token number">1</span></code>、<code class=" language-php"><span class="token number">0</span></code>、<code class=" language-php"><span class="token string">"1"</span></code> 以及 <code class=" language-php"><span class="token string">"0"</span></code>。</p><p><a name="rule-confirmed"></a></p><h4>confirmed</h4><p>验证字段值必须和 <code class=" language-php">foo_confirmation</code> 的字段值一致。例如,如果要验证的字段是 <code class=" language-php">password</code>,就必须和输入数据里的 <code class=" language-php">password_confirmation</code> 的值保持一致。</p><p><a name="rule-date"></a></p><h4>date</h4><p>验证字段值是否为有效日期,会根据 PHP 的 <code class=" language-php">strtotime</code> 函数来做验证。</p><p><a name="rule-date-format"></a></p><h4>date_format:<em>format</em></h4><p>验证字段值符合指定的日期格式 (<em>format</em>)。你应该只使用 <code class=" language-php">date</code> 或 <code class=" language-php">date_format</code> 当中的 <strong>其中一个</strong> 用于验证,而不应该同时使用两者。</p><p><a name="rule-different"></a></p><h4>different:<em>field</em></h4><p>验证字段值是否和指定的字段 (<em>field</em>) 有所不同。</p><p><a name="rule-digits"></a></p><h4>digits:<em>value</em></h4><p>验证字段值是否为 <em>numeric</em> 且长度为 <em>value</em>。</p><p><a name="rule-digits-between"></a></p><h4>digits_between:<em>min</em>,<em>max</em></h4><p>验证字段值的长度是否在 <em>min</em> 和 <em>max</em> 之间。</p><p><a name="rule-dimensions"></a></p><h4>dimensions</h4><p>验证的文件必须是图片并且图片比例必须符合规则:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'avatar'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'dimensions:min_width=100,min_height=200'</span></code></pre><p>可用的规则为: <em>min_width</em>,<em>max_width</em>,<em>min_height</em>,<em>max_height</em>,<em>width</em>,<em>height</em>,<em>ratio</em>。</p><p>比例应该使用宽度除以高度的方式出现。能够使用 3/2 这样的形式设置,也可以使用 1.5 这样的浮点方式:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'avatar'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'dimensions:ratio=3/2'</span></code></pre><p>Since this rule requires several arguments, you may use the method to fluently construct the rule:</p><p>由于此规则需要多个参数,因此您可以 <code class=" language-php"><span class="token scope">Rule<span class="token punctuation">::</span></span>dimensions</code> 方法来构造规则:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span>
<span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'avatar'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token scope">Rule<span class="token punctuation">::</span></span><span class="token function">dimensions<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">maxWidth<span class="token punctuation">(</span></span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">maxHeight<span class="token punctuation">(</span></span><span class="token number">500</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">ratio<span class="token punctuation">(</span></span><span class="token number">3</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="rule-distinct"></a></p><h4>distinct</h4><p>当你在验证数组的时候,你可以指定某个值必须是唯一的:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'foo.*.id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'distinct'</span></code></pre><p><a name="rule-email"></a></p><h4>email</h4><p>验证字段值是否符合 e-mail 格式。</p><p><a name="rule-exists"></a></p><h4>exists:<em>table</em>,<em>column</em></h4><p>验证字段值是否存在指定的数据表中。</p><h4>Exists 规则的基本使用方法</h4><pre class=" language-php"><code class=" language-php"><span class="token string">'state'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'exists:states'</span></code></pre><h4>指定一个特定的字段名称</h4><pre class=" language-php"><code class=" language-php"><span class="token string">'state'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'exists:states,abbreviation'</span></code></pre><p>有时,您可能需要指定要用于 <code class=" language-php">exists</code> 查询的特定数据库连接。你可以使用点「.」语法将数据库连接名称添加到数据表前面来实现这个目的:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'exists:connection.staff,email'</span></code></pre><p>如果您想自定义由验证规则执行的查询,您可以使用 <code class=" language-php">Rule</code> 类流畅地定义规则。在这个例子中,我们还将使用数组指定验证规则,而不是使用 <code class=" language-php"><span class="token operator">|</span></code> 字符来分隔它们:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span>
<span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token scope">Rule<span class="token punctuation">::</span></span><span class="token function">exists<span class="token punctuation">(</span></span><span class="token string">'staff'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">where<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$query</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">where<span class="token punctuation">(</span></span><span class="token string">'account_id'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="rule-file"></a></p><h4>file</h4><p>必须是成功上传的文件。</p><p><a name="rule-filled"></a></p><h4>filled</h4><p>验证的字段必须带有内容。</p><p><a name="rule-image"></a></p><h4>image</h4><p>验证字段文件必须为图片格式( jpeg、png、bmp、gif、或 svg )。</p><p><a name="rule-in"></a></p><h4>in:<em>foo</em>,<em>bar</em>,...</h4><p>验证字段值是否有在指定的列表里面。因为这个规则通常需要你 <code class=" language-php">implode</code> 一个数组,<code class=" language-php"><span class="token scope">Rule<span class="token punctuation">::</span></span>in</code> 方法可以用来流利地构造规则:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span>
<span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'zones'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token scope">Rule<span class="token punctuation">::</span></span><span class="token function">in<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'first-zone'</span><span class="token punctuation">,</span> <span class="token string">'second-zone'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="rule-in-array"></a></p><h4>in_array:<em>anotherfield</em></h4><p>验证的字段必须存在于 <em>anotherfield</em> 的值中。</p><p><a name="rule-integer"></a></p><h4>integer</h4><p>验证字段值是否是整数。</p><p><a name="rule-ip"></a></p><h4>ip</h4><p>验证字段值是否符合 IP address 的格式。</p><h4>ipv4</h4><p>验证字段值是否符合 IPv4 的格式。</p><h4>ipv6</h4><p>验证字段值是否符合 IPv6 的格式。</p><p><a name="rule-json"></a></p><h4>json</h4><p>验证字段是否是一个有效的 JSON 字符串。</p><p><a name="rule-max"></a></p><h4>max:<em>value</em></h4><p>字段值必须小于或等于 <em>value</em>。字符串、数值或是文件大小的计算方式和 <a href="#rule-size"><code class=" language-php">size</code></a> 规则相同。</p><p><a name="rule-mimetypes"></a></p><h4>mimetypes:<em>text/plain</em>,...</h4><p>验证的文件必须是这些 MIME 类型中的一个:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'video'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'mimetypes:video/avi,video/mpeg,video/quicktime'</span></code></pre><p>要确定上传文件的MIME类型,会读取文件的内容,并且框架将尝试猜测 MIME 类型,这可能与客户端提供的 MIME 类型不同。</p><p><a name="rule-mimes"></a></p><h4>mimes:<em>foo</em>,<em>bar</em>,...</h4><p>验证字段文件的 MIME 类型是否符合列表中指定的格式。</p><h4>MIME 规则基本用法</h4><pre class=" language-php"><code class=" language-php"><span class="token string">'photo'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'mimes:jpeg,bmp,png'</span></code></pre><p>即使你可能只需要验证指定扩展名,但此规则实际上会验证文件的 MIME 类型,其通过读取文件的内容以猜测它的 MIME 类型。</p><p>完整的 MIME 类型及对应的扩展名列表可以在下方链接找到:<a href="https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types"></a><a href="https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types">https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types</a></p><p><a name="rule-min"></a></p><h4>min:<em>value</em></h4><p>字段值必须大于或等于 <em>value</em>。字符串、数值或是文件大小的计算方式和 <a href="#rule-size"><code class=" language-php">size</code></a> 规则相同。</p><p><a name="rule-nullable"></a></p><h4>nullable</h4><p>验证的字段可以为 <code class=" language-php"><span class="token keyword">null</span></code>。这在验证基本数据类型,如字符串和整型这些能包含 <code class=" language-php"><span class="token keyword">null</span></code> 值的数据类型中特别有用。</p><p><a name="rule-not-in"></a></p><h4>not_in:<em>foo</em>,<em>bar</em>,...</h4><p>验证字段值是否不在指定的列表里。</p><p><a name="rule-numeric"></a></p><h4>numeric</h4><p>验证字段值是否为数值。</p><p><a name="rule-present"></a></p><h4>present</h4><p>验证的字段必须出现,并且数据可以为空。</p><p><a name="rule-regex"></a></p><h4>regex:<em>pattern</em></h4><p>验证字段值是否符合指定的正则表达式。</p><p><strong>Note:</strong> 当使用 <code class=" language-php">regex</code> 规则时,你必须使用数组,而不是使用管道分隔规则,特别是当正则表达式含有管道符号时。</p><p><a name="rule-required"></a></p><h4>required</h4><p>验证字段必须存在输入数据,且不为空。字段符合下方任一条件时即为「空」:</p><div class="content-list"><ul><li>该值为 <code class=" language-php"><span class="token keyword">null</span></code>.</li><li>该值为空字符串。</li><li>该值为空数组或空的 <code class=" language-php">可数</code> 对象。</li><li>该值为没有路径的上传文件。</li></ul></div><p><a name="rule-required-if"></a></p><h4>required_if:<em>anotherfield</em>,<em>value</em>,...</h4><p>如果指定的其它字段( <em>anotherfield</em> )等于任何一个 <em>value</em> 时,此字段为必填。</p><p><a name="rule-required-unless"></a></p><h4>required_unless:<em>anotherfield</em>,<em>value</em>,...</h4><p>如果指定的其它字段( <em>anotherfield</em> )等于任何一个 <em>value</em> 时,此字段为不必填。</p><p><a name="rule-required-with"></a></p><h4>required_with:<em>foo</em>,<em>bar</em>,...</h4><p>如果指定的字段中的 <em>任意一个</em> 有值且不为空,则此字段为必填。</p><p><a name="rule-required-with-all"></a></p><h4>required_with_all:<em>foo</em>,<em>bar</em>,...</h4><p>如果指定的 <em>所有</em> 字段都有值,则此字段为必填。</p><p><a name="rule-required-without"></a></p><h4>required_without:<em>foo</em>,<em>bar</em>,...</h4><p>如果缺少 <em>任意一个</em> 指定的字段,则此字段为必填。</p><p><a name="rule-required-without-all"></a></p><h4>required_without_all:<em>foo</em>,<em>bar</em>,...</h4><p>如果所有指定的字段 <em>都没有</em> 值,则此字段为必填。</p><p><a name="rule-same"></a></p><h4>same:<em>field</em></h4><p>验证字段值和指定的 字段( <em>field</em> ) 值是否相同。</p><p><a name="rule-size"></a></p><h4>size:<em>value</em></h4><p>验证字段值的大小是否符合指定的 <em>value</em> 值。对于字符串来说,<em>value</em> 为字符数。对于数字来说,<em>value</em> 为某个整数值。对文件来说,<em>size</em> 对应的是文件大小(单位 kb )。</p><p><a name="rule-string"></a></p><h4>string</h4><p>验证字段值的类型是否为字符串。如果你允许字段的值为 <code class=" language-php"><span class="token keyword">null</span></code> ,那么你应该将 <code class=" language-php">nullable</code> 规则附加到字段中。</p><p><a name="rule-timezone"></a></p><h4>timezone</h4><p>验证字段值是否是有效的时区,会根据 PHP 的 <code class=" language-php">timezone_identifiers_list</code> 函数来判断。</p><p><a name="rule-unique"></a></p><h4>unique:<em>table</em>,<em>column</em>,<em>except</em>,<em>idColumn</em></h4><p>在指定的数据表中,验证字段必须是唯一的。如果没有指定 <code class=" language-php">column</code>,将会使用字段本身的名称。</p><p><strong>指定一个特定的字段名称:</strong></p><pre class=" language-php"><code class=" language-php"><span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'unique:users,email_address'</span></code></pre><p><strong>自定义数据库连接</strong></p><p>有时,您可能需要为验证程序所做的数据库查询设置自定义连接。如上面所示,如上所示,将 <code class=" language-php">unique:users</code> 设置为验证规则将使用默认数据库连接来查询数据库。如果要修改数据库连接,请使用「点」语法指定连接和表名:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'unique:connection.users,email_address'</span></code></pre><p><strong>强迫 Unique 规则忽略指定 ID:</strong></p><p>有时候,你希望在进行字段唯一性验证时对指定 ID 进行忽略。例如,在「更新个人资料」页面会包含用户名、邮箱等字段。这时你会想要验证更新的 E-mail 值是否为唯一的。如果用户仅更改了名称字段而没有改 E-mail 字段,就不需要抛出验证错误,因为此用户已经是这个 E-mail 的拥有者了。若要用指定规则来忽略用户 ID,则应该把要发送的 ID 当作第三个参数:</p><p>为了指示验证器忽略用户的ID,我们将使用 <code class=" language-php">Rule</code> 类流畅地定义规则。 在这个例子中,我们还将通过数组来指定验证规则,而不是使用 <code class=" language-php"><span class="token operator">|</span></code> 字符来分隔:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span>
<span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'required'</span><span class="token punctuation">,</span>
<span class="token scope">Rule<span class="token punctuation">::</span></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">ignore<span class="token punctuation">(</span></span><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如果你的数据表使用的主键名称不是 <code class=" language-php">id</code>,则可以在调用 <code class=" language-php">ignore</code> 方法时指定列的名称:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token scope">Rule<span class="token punctuation">::</span></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">ignore<span class="token punctuation">(</span></span><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">,</span> <span class="token string">'user_id'</span><span class="token punctuation">)</span></code></pre><p><strong>增加额外的 Where 语句:</strong></p><p>你也可以通过 <code class=" language-php">where</code> 方法指定额外的查询约束条件。例如,我们添加 <code class=" language-php">account_id</code> 为 <code class=" language-php"><span class="token number">1</span></code> 约束条件:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token scope">Rule<span class="token punctuation">::</span></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">where<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$query</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">where<span class="token punctuation">(</span></span><span class="token string">'account_id'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre><p><a name="rule-url"></a></p><h4>url</h4><p>验证字段必需是有效的 URL 格式。</p><p><a name="conditionally-adding-rules"></a></p><h2><a href="#conditionally-adding-rules">按条件增加规则</a></h2><h4>当字段存在的时候进行验证</h4><p>在某些情况下,你可能 <strong>只想</strong> 在输入数据中有此字段时才进行验证。可通过增加 <code class=" language-php">sometimes</code> 规则到规则列表来实现:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$v</span> <span class="token operator">=</span> <span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'sometimes|required|email'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>在上面的例子中,<code class=" language-php">email</code> 字段的验证只会在 <code class=" language-php"><span class="token variable">$data</span></code> 数组有此字段时才会进行。</p><h4>复杂的条件验证</h4><p>有时候你可能希望增加更复杂的验证条件,例如,你可以希望某个指定字段在另一个字段的值超过 100 时才为必填。或者当某个指定字段有值时,另外两个字段要拥有符合的特定值。增加这样的验证条件并不难。首先,利用你熟悉的 <em>static rules</em> 来创建一个 <code class=" language-php">Validator</code> 实例:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$v</span> <span class="token operator">=</span> <span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required|email'</span><span class="token punctuation">,</span>
<span class="token string">'games'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required|numeric'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>假设我们有一个专为游戏收藏家所设计的网页应用程序。如果游戏收藏家收藏超过一百款游戏,我们会希望他们来说明下为什么他们会拥有这么多游戏。比如说他们有可能经营了一家二手游戏商店,或者只是为了享受收集的乐趣。为了在特定条件下加入此验证需求,可以在 <code class=" language-php">Validator</code> 实例中使用 <code class=" language-php">sometimes</code> 方法。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$v</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">sometimes<span class="token punctuation">(</span></span><span class="token string">'reason'</span><span class="token punctuation">,</span> <span class="token string">'required|max:500'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$input</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">games</span> <span class="token operator">>=</span> <span class="token number">100</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>传入 <code class=" language-php">sometimes</code> 方法的第一个参数是我们要用条件认证的字段名称。第二个参数是我们想使用的验证规则。<code class=" language-php">闭包</code> 作为第三个参数传入,如果其返回 <code class=" language-php"><span class="token boolean">true</span></code>,则额外的规则就会被加入。这个方法可以轻松的创建复杂的条件式验证。你甚至可以一次对多个字段增加条件式验证:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$v</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">sometimes<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'reason'</span><span class="token punctuation">,</span> <span class="token string">'cost'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'required'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$input</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">games</span> <span class="token operator">>=</span> <span class="token number">100</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 传入 <code class=" language-php">闭包</code> 的 <code class=" language-php"><span class="token variable">$input</span></code> 参数是 <code class=" language-php">Illuminate\<span class="token package">Support<span class="token punctuation">\</span>Fluent</span></code> 实例,可用来访问你的输入或文件对象。</p></blockquote><p><a name="validating-arrays"></a></p><h2><a href="#validating-arrays">验证数组</a></h2><p>验证基于数组的表单输入字段并不一定是一件痛苦的事情。要验证指定数组输入字段中的每一个 email 是否唯一,可以这么做:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token variable">$request</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'person.*.email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'email|unique:users'</span><span class="token punctuation">,</span>
<span class="token string">'person.*.first_name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'required_with:person.*.last_name'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>同理,你在语言文件定义验证信息的时候可以使用星号 <code class=" language-php"><span class="token operator">*</span></code> 字符,可以更加容易的在基于数组格式的字段中使用相同的验证信息:</p><pre class=" language-php"><code class=" language-php"><span class="token string">'custom'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'person.*.email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token string">'unique'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Each person must have a unique e-mail address'</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span></code></pre><p><a name="custom-validation-rules"></a></p><h2><a href="#custom-validation-rules">自定义验证规则</a></h2><p>Laravel 提供了许多有用的验证规则。但你可能想自定义一些规则。注册自定义验证规则的方法之一,就是使用 <code class=" language-php">Validator</code> <a href="/docs/5.4/facades">Facade</a> 中的 <code class=" language-php">extend</code> 方法,让我们在 <a href="/docs/5.4/providers">服务提供者</a> 中使用这个方法来注册自定义的验证规则:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 启动任意应用程序服务。
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">boot<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">extend<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">,</span> <span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">==</span> <span class="token string">'foo'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">/**
* 注册服务容器。
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">register<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>自定义的验证闭包接收四个参数:要被验证的属性名称 <code class=" language-php"><span class="token variable">$attribute</span></code>,属性的值 <code class=" language-php"><span class="token variable">$value</span></code>,传入验证规则的参数数组 <code class=" language-php"><span class="token variable">$parameters</span></code>,及 <code class=" language-php">Validator</code> 实例。</p><p>除了使用闭包,你也可以传入类和方法到 <code class=" language-php">extend</code> 方法中:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">extend<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token string">'FooValidator@validate'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h4>自定义错误消息</h4><p>另外你可能还需要为自定义规则来定义一个错误消息。这可以通过使用自定义内联消息数组或是在验证语言包中加入新的规则来实现。此消息应该被放在数组的第一级,而不是被放在 <code class=" language-php">custom</code> 数组内,这是仅针对特定属性的错误消息:</p><pre class=" language-php"><code class=" language-php"><span class="token string">"foo"</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">"你的输入是无效的!"</span><span class="token punctuation">,</span>
<span class="token string">"accepted"</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">":attribute 必须被接受。"</span><span class="token punctuation">,</span>
<span class="token comment" spellcheck="true">
// 其余的验证错误消息...</span></code></pre><p>当你在创建自定义验证规则时,你可能需要定义占位符来取代错误消息。你可以像上面所描述的那样通过 <code class=" language-php">Validator</code> Facade 来使用 <code class=" language-php">replacer</code> 方法创建一个自定义验证器。通过 <a href="/docs/5.4/providers">服务提供者</a> 中的 <code class=" language-php">boot</code> 方法可以实现:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">/**
* 启动任意应用程序服务。
*
* @return void
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">boot<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">extend<span class="token punctuation">(</span></span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">replacer<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">,</span> <span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$rule</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">str_replace<span class="token punctuation">(</span></span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><h4>隐式扩展功能</h4><p>默认情况下,若有一个类似 <a href="#rule-required"><code class=" language-php">required</code></a> 这样的规则,当此规则被验证的属性不存在或包含空值时,其一般的验证规则(包括自定扩展功能)都将不会被运行。例如,当 integer 规则的值为 null 时 <a href="#rule-unique"><code class=" language-php">unique</code></a> 将不会被运行:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$rules</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'unique'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token keyword">null</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">make<span class="token punctuation">(</span></span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">passes<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true"> // true</span></code></pre><p>如果要在属性为空时依然运行此规则,则此规则必须暗示该属性为必填。要创建一个「隐式」扩展功能,可以使用 <code class=" language-php"><span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">extendImplicit<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Validator<span class="token punctuation">::</span></span><span class="token function">extendImplicit<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">,</span> <span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">==</span> <span class="token string">'foo'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 一个「隐式」扩展功能只会 暗示 该属性为必填。它的实际属性是否为无效属性或空属性主要取决于你。</p></blockquote><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="http://weibo.com/wangkaibo">@王凯波</a></td><td><img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/1924_1487053084.jpeg?imageView2/1/w/100/h/100"></td><td>翻译</td><td>面向工资编程 😆 <a href="https://github.com/wangkaibo/">@wangkaibo</a></td></tr></tbody></table></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度