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?