Search code examples
laravelrules

Laravel custom rule if previous rules passed


In Request rules() I have

public function rules()
{
    return [
        'files' => ['nullable', 'array'],
        'files.*' => ['file','max:256000', new PostFiles],
        'body' => 'nullable|string',
        'public' => 'boolean',
    ];
}

I need PostFiles rule be executed only if all other checks regarding file are passed. Now I am getting error if I will send instead file array of strings and I don't want to duplicate 'file','max:256000' check in PostFiles in passes(){} method.

Laravel 7


Solution

  • found solution:

    just add bail

    https://laravel.com/docs/8.x/validation#stopping-on-first-validation-failure