Laravel 第一次失败后如何停止所有验证?

Art*_*sov 5 laravel

bailLaravel 中有一个函数可以在失败时停止验证。如果我想在第一次失败时停止所有验证怎么办?

例如:

$request->validate([
    'antibotprotection' => 'bail|required|exists:protectioncodes,protection_code|max:255',
    'email' => 'required|string|email|max:255|unique:users',
]);
Run Code Online (Sandbox Code Playgroud)

此代码将停止验证,antibotprotection但随后它将继续验证电子邮件并传递错误以查看有关电子邮件的信息,这违背了整个目的。validate第一次失败时,我将如何停止整个功能?

Que*_*ler 1

./app/Validation/BailingValidator.php

<?php

namespace App\Validation;

use Illuminate\Support\MessageBag;
use Illuminate\Validation\Validator;

class BailingValidator extends Validator
{
    /**
     * Determine if the data passes the validation rules.
     *
     * @return bool
     */
    public function passes()
    {
        $this->messages = new MessageBag;

        // We'll spin through each rule, validating the attributes attached to that
        // rule. Any error messages will be added to the containers with each of
        // the other error messages, returning true if we don't have messages.
        foreach ($this->rules as $attribute => $rules) {
            $attribute = str_replace('\.', '->', $attribute);

            foreach ($rules as $rule) {
                $this->validateAttribute($attribute, $rule);

                if ($this->shouldStopValidating($attribute)) {
                    break 2;
                }
            }
        }

        // Here we will spin through all of the "after" hooks on this validator and
        // fire them off. This gives the callbacks a chance to perform all kinds
        // of other validation that needs to get wrapped up in this operation.
        foreach ($this->after as $after) {
            call_user_func($after);
        }

        return $this->messages->isEmpty();
    }
}
Run Code Online (Sandbox Code Playgroud)

./app/Providers/AppServiceProvider.php

...
use App\Validation\BailingValidator;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Support\ServiceProvider;
...
    public function boot()
    {
        /**
         * @var \Illuminate\Validation\Factory $factory
         */
        $factory = resolve(Factory::class);

        $factory->resolver(function (Translator $translator, array $data, array $rules, array $messages, array $customAttributes) {
            return new BailingValidator($translator, $data, $rules, $messages, $customAttributes);
        });
    }
...
Run Code Online (Sandbox Code Playgroud)

./app/Http/Controller/SomeController.php

...
        $this->validate($request, [
            'foo' => ['bail', 'required'],
            'bar' => ['bail', 'required'],
        ]);
...
Run Code Online (Sandbox Code Playgroud)

{"message":"The given data was invalid.","errors":{"foo":["The foo field is required."]}}