Laravel 5 required_if验证总是失败

时间:2017-06-17 09:14:32

标签: php validation laravel-5

假设我有一个下拉列表district_code_c,其中一个选项是other,因此当用户选择other时,系统会显示名为district_other_c的输入和用户输入内容,我有一个这样的验证规则:

$present_fields_rules['district_other_c'] = 'required_if:district_code_c,other';
$validator = Validator::make(
  [
     'district_code_c' => $request->get('district_code_c'),
  ],
  [
     'district_code_c' => 'required',
     'district_other_c' => $present_fields_rules['district_other_c']
  ]
);

当我从下拉列表中选择other并注意输入时输入时,系统会显示*The Current address other city/district field is required when Current address city/district is other.* 但是我输入了一些内容,然后验证再次失败。

2 个答案:

答案 0 :(得分:0)

您在呈现的下拉列表中看到的是<option>的标签,该标签不一定与将提交的值相同。

答案 1 :(得分:0)

在应用验证规则之前,只需获取输入值:

$validator = Validator::make(
 [
   'district_code_c' => $request->get('district_code_c'),
   'district_other_c' => $request->get('district_other_c'),
 ],
 [
   'district_code_c' => 'required',
   'district_other_c' => $present_fields_rules['district_other_c']
 ]
);