Make a generic validation method in laravel

1

I am trying to improve my code a bit and since I have to validate several methods of several controllers, I would like to do a generic method in the Controller Controller, and redirect the validations there. I have tried several options but I can not make them work, I do not know very well why

In the "CONTROLLER" Controller

protected function validattion($request,$reglas,$mensajes = null)
{

    $validador = \Validator($request,$reglas,$mensajes = []);

    if ($validador->fails()) :
        return redirect()->back()->withInput()->withErrors($validador);
     endif;

}

and then from any other method of any other controller I call it that way

 public function crearEstudiantes(Request $request)
    {
        $reglas = 
          [
            'nombre' => 'min:2',
            'direccion' => 'min:2',
            'telefono' => 'min:2'
          ];

        $this->validattion($request->all(),$reglas);

         ////////////////////////
         CODIGO PARA CREAR ESTUDIANTE


    }

If I copy the content of the validattion method (located in the CONTROLER Controller) as created, create Students (or another method) in this way

/////////////////////////EDICION DE LA PREGUNTA 1///////////////////////////
     public function crearEstudiantes(Request $request)
        {
            $reglas = 
              [
                'nombre' => 'min:2',
                'direccion' => 'min:2',
                'telefono' => 'min:2'
              ];

               $validador = \Validator::make($request->all(),$reglas);

             if ($validador->fails()) :
                 return redirect()->back()->withInput()->withErrors($validador);
              endif;
        }


/////////////////////FIN DE EDICION DE LA PREGUNTA 1/////////////////////////

works perfect, that is validates, but when calling the validattion method within create Student does not give an error but simply does nothing and continues executing the code that exists in that method. How could I solve this?

    
asked by KurodoAkabane 08.04.2017 в 12:29
source

1 answer

1

The problem seems to be that you are not using a return in the method that calls the validation, nor are you throwing the validation exception, all this when an error occurs and it fails.

According to the Laravel documentation you have two options, although in practice you have more:

  • Use the trait ValidatesRequests in the base controller and thus use its validate() method that throws the respective validation exception:

    // ...
    use Illuminate\Foundation\Validation\ValidatesRequests;
    
    abstract class Controller extends BaseController
    {
         // ...
         use ValidatesRequests;
    
         public funcion crearEstudiantes(Request $request)
         {
             // ...
             $this->validate($request, [
                'nombre' => 'min:2',
                'direccion' => 'min:2',
                'telefono' => 'min:2'
             ]);
             // ...
         }
    
         // ...
    }
    
  • The second method proposed by the documentation to generate automatic redirection, in this case without using the trait, is to call the validate method in an instance of the validator:

    public function crearEstudiantes(Request $request)
    {
    
        Validator::make($request->all(), [
            'nombre' => 'min:2',
            'direccion' => 'min:2',
            'telefono' => 'min:2'
        ])->validate();
    
        // ...
    
    }
    
  • More information in the official documentation: link

        
    answered by 10.04.2017 / 12:53
    source