1
votes

In my Laravel 5.2 app, I have the following validation rules to check a field:

$rules = array(
    'file' => 'mimes:jpg,png,pdf,doc,docx',
    'file' => 'validate_file'
);

Now, the validate_file is a custom validation rule. I want to run it only only if the first mimes validation passes.

I know I could simply validate/redirect with errors - in two batches, first the mimes, then the validate_file, but it seems like an overkill.

Any way I can do it the smart way in Laravel 5.2?

1

1 Answers

2
votes

Use "bail" attribute. It will stop validation after first failure.

$this->validate($request, [
    'title' => 'bail|mimes:jpg,png,pdf,doc,docx|validate_file',
]);

https://laravel.com/docs/5.2/validation#validation-quickstart search for "bail"