Search code examples
laravellaravel-validation

Laravel validation required_without_all


Call me stupid, but I can't get it :) I want to set up validator rule, so it will pass only if one of the two fields is present (adgroup or all_adgroups).

Here is my controller:

 $this->validate($request,
        [
            'new_target_cpa_value' => 'required|numeric',
            'adgroups' => 'exists:google.ad_groups,id|required_without_all:all_agroups',
            'all_agroups' => 'required_without_all:adgroups'
        ]
    );
    dd($request->all());

Here is dd:

"_token" => "aHjluUXPuZpEbglmVt4UePhriGvRWDOjk3OgfF88" "new_target_cpa_value" => "123" "adgroups" => array:1 [▶] "all_agroups" => "1"


Solution

  • Try this:

     $this->validate($request,
            [
                'new_target_cpa_value' => 'required|numeric',
                'adgroups' => 'exists:google.ad_groups,id',
                'all_agroups' => 'required_unless:adgroups,null'
            ]
        );