Reputation: 15070
I have two validation rules that I want to apply to the 'metric_id' field:
$rules = ['metric_id' => 'exists:metrics,id|in:foo,bar'];
But instead of having to satisfy both rules, I want to satisfy one or the other (either exists in metrics table or is a specified value).
How do I do this? Do I have to make a custom validation rule?
Upvotes: 0
Views: 61
Reputation: 4128
I think the easiest way to do it is:
$metric_id_passes =
Validator::make([Input::get('metric_id')], ['metric_id' => 'exists:metrics,id'])->passes()
||
Validator::make([Input::get('metric_id'), ['metric_id' => 'in:foo,bar']])->passes();
Or you can wrap it in a custom validation rule:
Validator::extend('MetricIDExistsOrIn', function($attribute, $value, $parameters)
{
return Validator::make(['metric_id' => 'exists:metrics,id'], [ $value ])->passes() ||
Validator::make(['metric_id' => 'in:foo,bar'], [$value])->passes();
});
$rules = ['metric_id' => 'MetricIDExistsOrIn'];
Upvotes: 1