如果其他字段有价值,则需要一个字段

时间:2019-05-31 14:44:20

标签: php laravel validation

我有3个字段,分别为bank_nameaccount_nameaccount_number。如果其中两个字段具有价值,该如何要求其他两个字段?我现在的验证看起来像这样:

validate = [
    'account_name' => ['sometimes','nullable', 'max:50'],
    'bank_name' => ['sometimes','nullable', 'max:50'],
    'account_number' => ['sometimes','nullable', 'max:50'],
];

我怎么可能做到这一点?

1 个答案:

答案 0 :(得分:3)

您可以使用required_with

$validate = [
    'account_name' => ['sometimes','nullable', 'max:50', 'required_with:bank_name,account_number'],
    'bank_name' => ['sometimes','nullable', 'max:50', 'required_with:account_name,account_number'],
    'account_number' => ['sometimes','nullable', 'max:50', 'required_with:account_name,bank_name'],
];

使用上述规则,当3个字段之一具有值时,将需要另两个字段。

有关更多信息:https://laravel.com/docs/5.8/validation#rule-required-with