Intervention / validation

Missing Laravel Validation Rules
https://validation.intervention.io
MIT License
590 stars 67 forks source link

added empty_with validator #15

Closed willemo closed 7 years ago

willemo commented 7 years ago

I've added an empty_with validator that checks if either the field under validation is empty or all of the other specified fields. With this validator you can make an either [this field] or [that field] validation.

olivervogel commented 7 years ago

Can you describe a use case for this?

willemo commented 7 years ago

Sure! I'm using it for manual or stored filters when doing a GET request to my API:

You can either make this request: GET /endpoint?foo=bar&baz=123

Or make this request: GET /endpoint?filter_id=321

If you'd do both in the same request, the validator would fail.

You can set it up like so:

$validator = Validator::make($request->all(), [
    'foo' => 'string|nullable|empty_with:filter_id',
    'baz' => 'integer|nullable|empty_with:filter_id',
    'filter_id' => 'integer|nullable|exists:filters,id',
]);

The filter_id field doesn't need an empty_with validator, since that's being covered by the other fields having it.

olivervogel commented 7 years ago

Sounds good.