【问题标题】:Require a field if the value exists in another field, which is an array如果值存在于另一个字段中,则需要一个字段,该字段是一个数组
【发布时间】:2021-10-24 19:29:00
【问题描述】:

假设一个模型有一个状态属性,它包含一个字符串数组。如果此数组包含字符串名称“Other”,则应该需要 status_other 字段。我可以通过以下规则实现这一点:

'status' => 'nullable|array',
'status_other' => Rule::requiredIf(in_array('Other', $this->model->status))

有没有办法将 status_other 规则写成字符串?我试过了:

'status_other' => 'required_if:status,in:Other',

'status_other' => 'required_if:status,Other',

这两种方法都不起作用。

【问题讨论】:

  • that 之类的东西可能会有所帮助。

标签: laravel laravel-validation


【解决方案1】:

您可以使用'=='匹配数组中的字符串进行比较。

'status_other' => 'required_if:status,==,Other',

【讨论】:

  • == 如何检查“其他”是否在“状态”数组中?刚刚尝试了您的建议,但没有成功。
猜你喜欢
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多