I have to receive a POST request from a client to my REST app in laravel 5. I'm follow the documentation related to validation, it said that when is an AJAX request laravel do not generate a redirect response, instead it will generate a JSON response with errors(https://laravel.com/docs/5.5/validation#a-note-on-optional-fields). But when I make the call from a REST client I'm getting this response:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url=http://laraveltest.com:8080/test/public" />
<title>Redirecting to http://laraveltest.com:8080/test/public</title>
</head>
<body>
Redirecting to <a href="http://laraveltest.com:8080/test/public">http://laraveltest.com:8080/test/public</a>.
</body>
</html>
AdultoPost class *********************
class AdultoPost extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'Profesion' => "max:50",
"EstadoCivil" => "required",
"FamiliaReconstituida" => "required",
"SolteroConHijo" => "required",
"Viudo" => "required",
"NoHijos" => "required"
];
}
}
AdultoController **************
public function post(AdultoPost $request, Adulto $adulto, Paciente $paciente, Persona $persona)
{
$persona = Persona::create($request->all());
$adulto = Adulto::create($request->all());
$paciente = Paciente::create($request->all());
return $adulto;
}
If I change AdultoPost class for Request and put the validation rules inside the post function and catch a ValidationException I get this error response "The given data was invalid". Which is not what I want. I would like to send to the client which fields were invalid and why.
I'm making the request from REST API plugin of VSCODE, and from REST API TESTING extension from chrome, and now I'm installing POSTMAN to continue testing
POST http://laraveltest.com:8080/test/public/api/adulto
Content-Type: application/json
Accept: application/json
{
"FechaInicio":"2017-09-10",
"MotivoConsulta":"un motivo real",
"ComoConocio":"como conocio"
}