Support the ongoing development of Laravel.io →
Input
Last updated 3 months ago.
0
$rules['assigned_to_user'] = 'required_without:assigned_to_group';
$rules['assigned_to_group'] = 'required_without:assigned_to_user';

https://laravel.com/docs/5.2/validation#rule-required-without

Last updated 6 years ago.
0

elite123 said:

$rules['assigned_to_user'] = 'required_without:assigned_to_group'; $rules['assigned_to_group'] = 'required_without:assigned_to_user';

https://laravel.com/docs/5.2/validation#rule-required-without

The strange thing about it is that validation fails if i'm setting no user and no group, but if i select both - validation passes.

Is it ok? Should i add more rules to validate input?

0

stalker said:

The strange thing about it is that validation fails if i'm setting no user and no group, but if i select both - validation passes.

Yes, unfortunately to my knowledge there are no built-in validation rules that require a field is absent, so you'll either have to create your own custom validation rule or find another workaround.

0

Here's how I'd accomplish it as a custom rule. The rule itself is super simple, but it unfortunately requires the duplication of a number of protected Validator-internal functions. (Those could be written much shorter inline, but I prefer to keep the structure the same in case I choose to switch to extending the Validator itself. Also then the logic is easier to follow because it parallels existing rules.)

https://gist.github.com/tdhsmith/df797334060462936fb62acb5fdf5489

Last updated 6 years ago.
0

tdhsmith said:

Here's how I'd accomplish it as a custom rule. The rule itself is super simple, but it unfortunately requires the duplication of a number of protected Validator-internal functions. (Those could be written much shorter inline, but I prefer to keep the structure the same in case I choose to switch to extending the Validator itself. Also then the logic is easier to follow because it parallels existing rules.)

https://gist.github.com/tdhsmith/df797334060462936fb62acb5fdf5489

Wow! It works perfectly and it's clear to me. Thank you very much!

0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

stalker stalker stalker Joined 5 Apr 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.