如何验证取决于PHP Laravel中的其他值?

时间:2018-10-17 05:53:14

标签: php laravel validation

我在laravel中有这样的验证

$this->validate($request, [
   "brief_table" => "required"
]);

然后我要检查其他验证取决于brief_table的值 就像如果brief_table的值是我要检查的“徽标”

[
            "service" => "required",
            "category" => "required",
            "logo" => "required",
            "description" => "required",
            "industry" => "required",
            "vision_mission" => "required",
            "logo_format" => "required",
            "symbol" => "required",
            "color" => "required",
            "clr" => "required",
            "designer_note" => "required",
            "business_card" => "required"
        ]

如果简要值是年度报告,那么我想应用其他规则

但是我需要检查brief_table值是否存在。

我尝试使用像这样的双重验证

$this->validate($request, ["brief_table" => "required"]);
$this->validate($request, $this->getValidationRule($request->brief_table));

第一个正在工作,但是第二个没有工作。

1 个答案:

答案 0 :(得分:1)

难道您不只是检查brief_table的值,然后根据结果应用不同的规则吗?

这里,如果brief_table ==标志,则适用第一个规则。如果还有其他问题(包括null),则应遵循其他规则。

if ($request->input('brief_table') == 'logo') {
    $this->validate($request, [
        "brief_table" => "required",
        "service" => "required",
        "category" => "required",
        "logo" => "required",
        "description" => "required",
        "industry" => "required",
        "vision_mission" => "required",
        "logo_format" => "required",
        "symbol" => "required",
        "color" => "required",
        "clr" => "required",
        "designer_note" => "required",
        "business_card" => "required"
    ]);
} else {
    $this->validate($request, [
        "brief_table" => "required",
        ... other rules ...
    ]);
}