Support the ongoing development of Laravel.io →
posted 8 years ago
Input
Last updated 2 years 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 8 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 8 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!

Eventy

Your banner here too?

stalker stalker Joined 5 Apr 2016

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.