DaemonKeen wrote a reply+100 XP
2mos ago
I've came across the same issue and solved it by creating a custom validation rule
<?php
namespace App\Rules;
use Closure;
use Illuminate\Validation\Validator;
use Illuminate\Contracts\Validation\{
ValidationRule,
ValidatorAwareRule
};
class StopOnFailure implements ValidationRule, ValidatorAwareRule
{
protected Validator $validator;
public $implicit = true;
public function setValidator(Validator $validator): static
{
$this->validator = $validator;
return $this;
}
public function __construct(
protected string $dependentField,
) {}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if ($this->validator->errors()->has($this->dependentField)) {
throw new \Illuminate\Validation\ValidationException($this->validator);
}
}
}
that I've attached to the upcoming field with the field we want to stop on as parameter. Now when validator encounter the StopOnFailure it will look into the error bag for the 'driver' and throw exception.
return [
'driver' => ['required', Rule::enum(ServiceDriver::class)],
'name' => [new StopOnFailure('driver'), 'required', 'string', 'max:255']
];
I think this solution works but it isn't optimal. It bypasses Laravel's standard error collection and jumps straight to the response which can lead to issues.
DaemonKeen wrote a comment+100 XP
3mos ago
DaemonKeen wrote a reply+100 XP
3mos ago