0

I'm building a small application to store contacts in the database, I've finished the GET/POST routes, and worked fine, now I'm on the API routes (in order to use AJAX calls). I can store the information if all fields are present in the POST request, nonetheless, If I want to send messages back to the call (to send feedback about why the contact hasn't been stored) the response is sending me to the main route www.myapp.com (with no messages) and I want to send a json back with the "reason".

At this moment I only validate if the 'nombre', 'correo', 'telefono' have information with standard Laravel's request validate method.

This is my LeadController

public function storeApi(Request $request)
    {
        $request -> validate([
            'nombre' => 'required',
            'correo' => 'required' ,
            'telefono' => 'required'

        ]);

        if(Lead::create($request->all())){

            $result[] = ['saved' => true];

        }else{

            $result[] = ['saved' => false,
            'reason' => 'Some data is missing'];
            return response()-> json($result);
            
        };

        return response()-> json($result);

    }

When the record is stored, it does send back the Json {'saved' : true} but when fails It just sends you back to the '/' Route: www.myapp.com

How can I send the messages back to the POST call?

Alexandro Giles
  • 497
  • 5
  • 18
  • 1
    `$request->validate()` performs a redirect if data is invalid. See this answer: https://stackoverflow.com/a/52665249/3965631 (And/or other answers on that page) – Tim Lewis Jul 12 '21 at 19:03
  • 1
    Thank you I just realize that if is not valid it throws an exception, since I don't have any CATCH it just sends me back to the main route: '/' thank you – Alexandro Giles Jul 12 '21 at 19:14

2 Answers2

1

It is redirecting back to "/" because $request->validate() method throws \Illuminate\Validation\ValidationException exception..

There are try ways to handle this request.

  1. Put try catch block around your validate code
  2. Or Handle this expection in app\Exception\Handler.php, and return the response in JSON format.
Vishal
  • 61
  • 7
  • That was happening, thank you for pointing me on the right direction, I just answer my own question, using a different approach but using the comment you just wrote. – Alexandro Giles Jul 12 '21 at 19:15
  • If you use any of the above mention methods, you can still use your original Code that you posted earlier. – Vishal Jul 12 '21 at 19:21
  • Oh I see, this way I can send my own custom messages, I was missing that. If I add the Catch block I can send a json response? – Alexandro Giles Jul 12 '21 at 19:25
  • Yes you can try that.. basically when ever the validation fails, and if not handled in the current code, it will go in Handler.php, as this is where all the unhandled exception goes. However, you if you add try, catch block, it will be handled in the current code, and wont go in Handler.php – Vishal Jul 12 '21 at 19:27
  • If you suggested answer worked for you, you can also close this question, after accepting the answer.. – Vishal Jul 12 '21 at 19:37
0

After some further reading I just change the way the information is validated using the Validator Class:

    public function storeApi(Request $request)
    {
        $validator = \Validator::make($request->all(), ['nombre' => 'required', 'correo' => 'required', 'telefono' => 'required']);

        if($validator->fails()){

            return response()->json($validator->errors(), 422);

        }else {

            //ready to store 

        }
    }

This way I don't let the ValidationException exception occurs before sending the feedback to the call.

Alexandro Giles
  • 497
  • 5
  • 18