framework
framework copied to clipboard
[12.x] Stops validation if a custom rule specifies it
Changes
- Adds a
Illuminate\Contracts\Validation\StopUponFailurecontract. - Changes
\Illuminate\Validation\Validator::shouldStopValidating()to accept two arguments, the new one being the rule - Changes
\Illuminate\Validation\Validator::shouldStopValidating()to check if it's an invokable rule and if the custom rule it's using implements theIlluminate\Contracts\Validation\StopUponFailurecontract.
Why
This would allow for more control over when validation rules fail and when they should continue. Instead of only relying on bail when one rule can trigger a failure to run all further attributes.
Example use:
class RunAntiVirusScan implements ValidationRule, StopUponFailure
{
public function shouldStop(): bool
{
return true;
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if ($scan->fails()) {
$fail(':attribute is not safe');
}
}
}
$request->validate([
'image' => ['image', new RunAntiVirusScan(), 'max:1024'],
]);
It feels a bit odd to have an interface called StopUponFailure but then have to implement a method which presumably will almost always return true. 🤔
@taylorotwell I did consider that It could just be an interface. The user might want to force that condition only in circumstances based on whether they felt it was risky or not to continue validation of that field was where my mind was at. I'm thinking where using AI/ML and you've got a score from a service and don't want to continue processing a file further.
Maybe a new name for the interface would help. MightStop as such for the interface and then a shouldStop method.
Maybe a new name for the interface would help.
MightStopas such for the interface and then ashouldStopmethod.
What about Stoppable?
We use the term bail in other parts for rules, so maybe Bailable or IsBailable gives better context
Why not name it: StopOnFailure and check if you implemented the empty interface, if so.. Stop on failure then.
I think @flavio-schoute is the best solution. We can later add Bailable interface that allows a method for conditional bail?