Laravel 5.4 "Complex Conditional Validation"

I was trying to make a complex validation with a Request file

"algunTexto"        =>    "required" ,
"archivo_texto"     =>    "required_if:algunTexto,si|file|mimes:doc,docx,pages,txt|max:10000" ,

It wasn't working, because the request was returning errors that the file must be "doc, docx, etc." and the input must be a file, so I decided to try adding bail

"archivo_texto"     =>    "bail|required_if:algunTexto,si|file|mimes:doc,docx,pages,txt|max:10000" ,

Now the "doc, docx" error was no longer appearing but the other one that the input must be a file was still there.

I made a research and ended up using this:

    $data = $request->all();
    $validator = validator($data,
        [
            'archivo_texto' => 'required_if:algunTexto,si'
        ]
    );
    $res = $validator->sometimes('archivo_texto', 'file|mimes:doc,docx,pages,txt|max:10000', function($data){
       return $data->algunTexto == 'si';
    });

But now, even if the radio button named "algunTexto" equals "si", and I don't upload any file on "archivo_texto", I get no error.

What am I doing wrong?

Upvotes: 0

Views: 350

Answers (1)

Now it works with the last attemp I made, I missed these lines:

if($validator->fails()){
    return back()->withErrors($validator);
}

I thought that it would return automatically with errors just like a Request does, sorry.

Upvotes: 1

Related Questions