lesssugar
lesssugar

Reputation: 16181

Laravel: Apply validation rule only if the another one passed

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?

Upvotes: 1

Views: 2745

Answers (1)

Mirr
Mirr

Reputation: 131

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"

Upvotes: 2

Related Questions