阅读(2169) (0)

Laravel 8 字段包含特定值跳过验证

2021-06-28 14:47:51 更新

如果另一个字段具有给定的值,您可能偶尔希望不验证给定字段。您可以使用 exclude_if 验证规则来实现这一点。在本例中,如果 has_appointment 字段的值为 false ,则不会验证 appointment_datedoctor_name 字段:

$v = Validator::make($data, [
    'has_appointment' => 'required|bool',
    'appointment_date' => 'exclude_if:has_appointment,false|required|date',
    'doctor_name' => 'exclude_if:has_appointment,false|required|string',
]);

您可以使用 exclude_unless 规则来验证给定字段,除非另一个字段具有给定的值:

$v = Validator::make($data, [
    'has_appointment' => 'required|bool',
    'appointment_date' => 'exclude_unless:has_appointment,true|required|date',
    'doctor_name' => 'exclude_unless:has_appointment,true|required|string',
]);