Variable validate returning wrong answer

Asked

Viewed 128 times

2

I have a 2 check uploads on a form with the validate in the following function:

public static function validarComprovantes($request)
{
    $request->validate([
        'pagamento' => 'required|mimes:pdf,jpg,png,jpeg,doc|file|size:max:20000',
        'sentenca' => 'required|mimes:pdf,jpg,png,jpeg,doc|file|size:max:20000',
    ],[
        'pagamento.required' => 'Você deve inserir o comprovamente de pagamento',
        'pagamento.mimes' => ' Você deve inserir um documento nos seguintes formatos: pdf,jpg,png,jpeg,doc',
        'pagamento.uploaded' => 'O arquivo de pagamento deve possuir no máximo 20MB',
        'sentenca.required' => 'Você deve inserir a sentença da causa',
        'sentenca.mimes' => ' Você deve inserir um documento nos seguintes formatos: pdf,jpg,png,jpeg,doc',
        'sentenca.uploaded' => 'O arquivo da sentença deve possuir no máximo 20MB',
    ]);
}

The following happens when I put the payment above 20MB and the sentence within the allowed is returned me the following error with the first right configuration, however returns the disfigured sentence and the mimes that in case I put(jpg).

inserir a descrição da imagem aqui

When I do the opposite it returns the error the contrary way, someone would know why the validation is automatically returning the other error?

  • 1

    Friend, I haven’t exactly understood your mistake yet. Could you explain in another way so that I can help you.

  • Vitor, I used separate validations at the end. The validate was giving error in validating two files, one valid and the other not.. he returned error to the two

1 answer

0

According to Laravel by the documentation follows example that the Laravel da:

 public static function validarComprovantes($request)
    {

$messages = [
                'pagamento.required' => 'Você deve inserir o comprovamente de pagamento',
                'pagamento.mimes' => ' Você deve inserir um documento nos seguintes formatos: pdf,jpg,png,jpeg,doc',
                'pagamento.uploaded' => 'O arquivo de pagamento deve possuir no máximo 20MB',
                'sentenca.required' => 'Você deve inserir a sentença da causa',
                'sentenca.mimes' => ' Você deve inserir um documento nos seguintes formatos: pdf,jpg,png,jpeg,doc',
                'sentenca.uploaded' => 'O arquivo da sentença deve possuir no máximo 20MB',
    ];

$roles = [
            'pagamento' => 'required|mimes:pdf,jpg,png,jpeg,doc|file|size:max:20000',
            'sentenca' => 'required|mimes:pdf,jpg,png,jpeg,doc|file|size:max:20000',
        ];

   $validation =  Validator::make($request, $roles, $messages);
    }

Reference: https://laravel.com/docs/5.8/validation#custom-error-messages

  • The second array is the error messages referring to the parameters that were inserted in the first array of the method validate.

  • What you put in already accuses error, since the second is the answer for each validation of the first

  • 1

    @Heleno you are customizing wrong I believe I’ll take a closer look and update my answer to help you.

  • Thank you, I’m trying to find a solution, because it was in fact to return only the wrong validation.

  • 1

    I found the problem I will edit the answer a moment

  • I’m on hold, thank you

  • I hope to have helped you with this doubt but according to one of the ways to make a custom is this way but in the documentation has to use parameters "custom" =>, as I do not use much custom messages Laravel so it is something for me very vague

  • unfortunately, it did not take..

  • What mistake you’re making?

  • You’re using Laravel en?

  • The Laravel only validates as I understood the types of inputs, but take a look at this: https://stackoverflow.com/questions/36265166/laravel-how-to-make-custom-validator

  • Some of these links can help you: https://stackoverflow.com/questions/45007905/custom-message-laravel-validation

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.