【发布时间】:2020-06-03 18:15:37
【问题描述】:
我正在尝试在 Laravel 中使用有点复杂的“或”条件进行输入验证。
如果输入的值存在于特定表中或者其值为“其他”,我需要验证器来验证输入(让它通过)。
到目前为止,我有:
$validator = Validator::make($data, [
...
'doc_organization' => ['required_with:rb_regist,doctor, exists:user_organizations,full_name'], // TODO: must exist in user_organizations table or be "other"
'doc_custom_organization' => ['required_if:doc_organization,other', 'max:160'],
...
我看了一下 Laravel 的自定义验证规则,有条件地添加规则等等,还有这些帖子:
Validation rules required_if with other condition (Laravel 5.4)
但我似乎无法提出一个自定义规则,在该规则中我不会查询整个表以了解名称是否存在(以防它不是“其他”)。这会使规则过于复杂,无法达到目的。
我的另一个解决方案是在 user_organizations 表中添加一个名为“other”的条目,这并不理想。
我错过了什么吗? 如何在没有复杂的自定义验证规则的情况下创建我想要的条件?
非常感谢。
【问题讨论】:
-
不要从前面发送
full_name,只发送ID。选项other将不发送任何ID(空/空)。在这种情况下,使用存在就足够了。
标签: php laravel validation