How to combine Laravel validation required_if and required_without_all rules?

Viewed 1408

I have a situation with a subscription form, which must have different validation rules depending on user selection. I almost complete this, but I'm stuck in a point which need a combination of rules that I think I can't get with predefined laravel rules.

As shown in the following chart, the point is when a user select invoicing preferences, with options Digital and Printed, if user option is Printed I need at least one physical address, so street address field group OR district address fields group must be mandatory.

Flow chart related to conditional validation I need to achieve

Mandatory field unless other field is filled can be achieved by required_without_allrule, so I've trying with no success, a combination of required_if and required_without_allrules, like the following example:

public function rules()
{
return [
...
    'invoicing_preferences' => 'required',
    'invoicing_email' => 'email|required_if:invoicing_preferences,digital',
    'invoicing_street_name' => 'string|required_if:invoicing_preferences,printed|required_without_all:invoicing_district,invoicing_parcel',
    'invoicing_street_number' => 'number|required_if:invoicing_preferences,printed|required_without_all:invoicing_district,invoicing_parcel',
    'invoicing_street_flat' => 'number|required_if:invoicing_preferences,printed|required_without_all:invoicing_district,invoicing_parcel',
    'invoicing_street_dep' => 'alpha_num|required_if:invoicing_preferences,printed|required_without_all:invoicing_district,invoicing_parcel',
    'invoicing_district' => 'alpha_num|required_if:invoicing_preferences,printed|required_without_all:invoicing_street_name, invoicing_street_number; invoicing_street_flat,invoicing_street_dep',
    'invoicing_parcel' => 'alpha_num|required_if:invoicing_preferences,printed|required_without_all:invoicing_street_name, invoicing_street_number; invoicing_street_flat,invoicing_street_dep',

...
    ];
}

This combination doesn't work because always results in the required_with_allrule no matter if I've checked digital at the first point.

1 Answers
Related