【问题标题】:Laravel required_if does not accept other parametersLaravel required_if 不接受其他参数
【发布时间】:2018-04-30 04:39:17
【问题描述】:

我对 Laravel 的验证有一些问题。我正在使用 required_if 验证器。

我想要实现的目标:hilightColorBooltrue 时,hilightColor 应该是必需的,应该是integermin:1max:5 之间。

...
'hilightColorBool' => 'required|boolean',
'hilightColor' => 'required_if:hilightColorBool,1|integer|min:1|max:5'
...

但是当我像上面那样做时,它总是返回 hilightColor 是布尔值,当我从 hilightColor 中删除整数、最小值和最大值时效果很好,但无论如何我都需要验证它是否是 1 到 5 之间的整数. 我觉得很奇怪。

【问题讨论】:

  • 试试看,可能有用:` 'hilightColor' => 'required_if:hilightColorBool|integer|min:1|max:5'`
  • 不幸的是它不起作用 - “验证规则 required_if 需要至少 2 个参数”。

标签: php laravel laravel-5.5


【解决方案1】:

继续我的评论,我可以这样做:

$rules = [
    'hilightColorBool' => 'required|boolean',
];

if (is_bool($request->get('hilightColorBool'))) {
    $rules['hilightColor'] = 'required|integer|min:1|max:5';      
}

然后简单地做:

$request->validate($rules);

【讨论】:

  • 是的,我是根据你的回答做的,但应该是 if (is_bool($request->get('hilightColorBool'))) 而不是 if ($request['hilightKidsWithNonstandardDiet'] == true)。谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-07-08
  • 2019-06-05
  • 2013-06-14
  • 2019-06-26
  • 2022-12-08
  • 2015-07-06
  • 2014-12-19
  • 1970-01-01
相关资源
最近更新 更多