Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

338
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda