Laravel 5。如何在请求中使用 'sometimes' 验证规则?

Laravel 5. How to use 'sometimes' validation rule in requests?

我正在使用位于 App\Http\Requests 文件夹中的 Post***Request.php。我有一个表格,只有当我的用户进入他的国家巴基斯坦时,我才需要城市字段。否则这些字段可以为空。

    $city1Rule = 'required_without_all:city1,city2,city3|different:city2|different:city3|in:'.implode (",", $cities);

    $city2Rule = 'required_without_all:city1,city2,city3|different:city1|different:city3|in:'.implode (",", $cities);

    $city3Rule = 'required_without_all:city1,city2,city3|different:city2|different:city1|in:'.implode (",", $cities);

    return [
        'country' => 
        'city1' => $city1Rule,
        'city2' => $city2Rule,
        'city3' => $city3Rule,
        'other_rules' => 'other_rules',         
        ];

我只想在这个文件中工作。我该如何修改它?

您可以使用 required_if:

required_if:country,Pakistan