在 Laravel 8.3 中,他们引入了一个新功能stopOnFirstFailure,一旦规则失败,就会完全停止验证。我想在Laravel 7中使用这个功能。在检查Laravel 8的vendor/laravel/framework/src/Validation/Validator.php时,我发现stopOnFirstFailure只是在Validator.php的passs函数中添加了一个if语句,如果受保护的变量stopOnFirstFailure为 true,则中断验证循环。是否可以通过扩展/覆盖 Validator.php 类在 Laravel 7 中实现这些?我一直在研究扩展 Laravel 核心类,并偶然发现了这篇文章,但它有点令人困惑,因为该文章只展示了如何重写特定函数。就我而言,我需要声明一个受保护的变量,覆盖一个函数并声明一个新函数。
Laravel 8 Validator.php 代码:
声明变量:
/**
* Indicates if the validator should stop on the first rule failure.
*
* @var bool
*/
protected $stopOnFirstFailure = false;
Run Code Online (Sandbox Code Playgroud)
stopOnFirstFailure函数:
/**
* Instruct the validator to stop validating after the first rule failure.
*
* @param bool $stopOnFirstFailure
* @return $this
*/
public function …Run Code Online (Sandbox Code Playgroud)