0

我想添加一个带有参数的自定义验证规则,并且在 Laravel 文档https://laravel.com/docs/5.8/validation#custom-validation-rules中没有验证参数选项。我想要一个像required_if.

4

2 回答 2

2

您可以在构造函数中传递它

new CustomRule($parameter);
class CustomRule implements Rule
{
    public function __construct($parameter)
    {
        $this->parameter = $parameter;
    }

...
于 2019-06-22T22:36:06.137 回答
0

您可以在验证器实例上使用有时方法。使用所需的规则并在函数中添加您的条件,如下所示:

$v->sometimes('reason', 'required|max:500', function ($input) {
    return $input->games >= 100;
});

上面的例子会检查游戏数是否大于等于 100,然后用户需要有理由,否则会触发错误。

该示例来自 Laravel 文档,您可以在此处找到

于 2019-06-22T21:00:40.250 回答