今のところ標準機能としては存在しないので,ベタ書きよりもマシになるように,自分で拡張して DRY 性を高めるぐらいの工夫しかできないと思います。
<?php
namespace App\Extensions\Validation;
use Illuminate\Validation\Validator as BaseValidator;
class Validator extends BaseValidator
{
/**
* @var bool|string[]
*/
protected $stopOnFirstFailure = false;
/**
* @param bool|string[] $stopOnFirstFailure
* @return $this
*/
public function stopOnFirstFailure($stopOnFirstFailure = true)
{
$this->stopOnFirstFailure = $stopOnFirstFailure;
return $this;
}
public function passes(): bool
{
$this->messages = new MessageBag;
[$this->distinctValues, $this->failedRules] = [[], []];
// 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) {
if ($this->shouldBeExcluded($attribute)) {
$this->removeAttribute($attribute);
continue;
}
// ↓ここを改造
if ($this->messages->isNotEmpty() && $this->determineStopOnFisrtFailure($attribute)) {
break;
}
foreach ($rules as $rule) {
$this->validateAttribute($attribute, $rule);
if ($this->shouldBeExcluded($attribute)) {
$this->removeAttribute($attribute);
break;
}
if ($this->shouldStopValidating($attribute)) {
break;
}
}
}
// 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) {
$after();
}
return $this->messages->isEmpty();
}
protected function determineStopOnFisrtFailure(string $attribute): bool
{
if (is_array($this->stopOnFirstFailure) && in_array($attribute, $this->stopOnFirstFailure, true)) {
return true;
}
return (bool)$this->stopOnFirstFailure;
}
}
<?php
namespace App\Extensions\Validation;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Factory;
class ValidationServiceProvider extends ServiceProvider
{
public function boot(Factory $factory): void
{
$factory->resolver(fn (...$args) => new Validator(...$args));
}
}
これをサービスプロバイダに登録すれば,自分で用意した拡張 Validator クラスが使用されるので,stopOnFirstFailure
の対象に string[]
を指定することができるようになるでしょう。