$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
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?
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.
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
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!
Sign in to participate in this thread!