Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

334
Vistas
Avoid Laravel redirect after validation

I'm making a simple validation on my Laravel controller:

public function updateInfo(Request $request) {

        $validator = $request->validate([
            "name" => ["string", "nullable"],
            "email" => ["email", "nullable"],
            "currentPassword" => ["password:api"]
        ]);

        return json_encode($validator);
}

My problem is that this validation redirects me to the home if fails, and i'm making the request via AJAX, i know that Laravel detects when a request is via Ajax, but it only works if is a normal request (the typical request in which i send the headers with Content Type application/json and in the body i sent a normal JSON

But Laravel is not able of detect when te Ajax request is not of Content Type application/json, i'm using the FormData() object of JavaScript, so, i don't sent the header Content Type application/json, and in the body i sent the FormData object.

I think it's simulate a typical HTTP request reloading the page, and for that reason Laravel is not able of detect it, but it gives me troubles with the ajax response because Laravel response the redirect and in my Ajax response i'm catching the HTML Code of the home page.

What can i do?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use validator() helper function with validate() method it will help you with both AJAX calls and normal calls

validator($request->all(), [
    "name" => ["string", "nullable"],
    "email" => ["email", "nullable"],
    "currentPassword" => ["password:api"]
])->validate();
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda