0

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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.