How to use make:request Laravel to clear the controller

Asked

Viewed 169 times

1

Laravel 5.5 I am using the validations as follows, directly in the controller

$this->validate($req,[
        'nome' => 'required|min:10',
        'cpf' => 'required|size:14',
        'agencia' => 'required',
    ],[
        'nome.required'=>'Preencha um nome',
        'nome.min'=>'Campo nome: Minimo de 10 Caracteres',
        'cpf.size' => 'Preencha corretamente o CPF',
        'required' => 'Campo Necessário',
    ]);

And in the views are either in the list or in the input field the error messages:

@if(count($errors) > 0 )
    <div class="alert alert-danger">
        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
  @endif

I would like to know how to use make:request and put in the functions of Ules and messages because when I put, when submitting the form occurs authentication error in the save controller I place the request and import the same

public function salvar(TesteValidaRequest $req){...}

in the request file

public function rules()
{
    return [
        'nome' => 'required|min:10'
    ];
}
public function messages()
{
return [
    'nome.required' => 'Necessário preencher um nome',
    'nome.min'  => 'Nome minimp de 10 Caracteres',
];

The doubt is that is appearing unauthorized action error when submitting the form, even Authorize() returning false as it says in the documentation, and also would like to know if to pass the errors to the views is the same way, tried through the documentation and could not.

  • the authorize has to be true for you to be able to use; regarding the error display the way you are doing is the most recommended.

  • the direct way in the controller is fully functional, but I would like to put in the requests, as it says in the documentation that clears the controller code.

  • Have you created the public Function Authorize() { Return true; } method in Testevalidarequest? If this method returns false which is the default, you will not be able to submit.

  • Thanks! worked now with changing the method to true.

1 answer

0

the method authorize in your class TesteValidaRequest must return true so that you can use this class as a validator.

public function authorize() {
    return true;
}

Browser other questions tagged

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