Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
La alerta de formulario vacío de Ajax no se muestra correctamente

Estoy tratando de generar un cuadro de texto de "campos faltantes" en mi formulario si los campos están vacíos, aquí están mis códigos para el contexto:

Controlador/Función

 public function store(Request $request) { $validator = Validator::make($request->all(), [ 'category_name'=>'required|max:191', 'category_description'=>'required|max:191', ]); if($validator->fails()) { return response()->json([ 'status'=>400, 'erros'=>$validator->messages(), ]); } else { $category = new HmsBbrCategory; $category->category_name = $request->input('category_name'); $category->category_description = $request->input('category_description'); $category->save(); return response()->json([ 'status'=>200, 'message'=>'Category Added!', ]); } }

AJAX

 <script> $(document).ready(function (){ $(document).on('click', '.add_category', function(e){ e.preventDefault(); var category_data = { 'category_name': $('.category_name').val(), 'category_description': $('.category_description').val(), } //token taken from laravel documentation $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); // console.log(category_data); $.ajax({ type: "POST", url: "/clinical/bbr-category-configuration", data: "category_data", dataType: "json", success: function (response){ if(response.status == 400) { $('#saveform_errList').html(""); $('#saveform_errList').addClass('alert alert-danger'); $.each(response.errors, function (key, err_values) { $('#saveform_errList').append('<li>'+err_values+'</li>'); }); } } }); }); }); </script>

También emito la identificación del error agregando esto a mi modal:

 <ul id="saveform_errList"></ul>

Lo probé en mi consola si funciona el error 400, y lo hace junto con mostrar qué campos faltan, esto se hace simplemente agregando console.log(response); en mi ajax

1

así que intenté mostrar este mismo error pero en el modal mismo, así que dentro de if(response.status==400) agregué lo siguiente como se muestra arriba:

 if(response.status == 400) { $('#saveform_errList').html(""); $('#saveform_errList').addClass('alert alert-danger'); $.each(response.errors, function (key, err_values) { $('#saveform_errList').append('<li>'+err_values+'</li>'); }); }

Sin embargo, solo obtengo la interfaz de usuario de peligro de alerta:

2

¿Qué me estoy perdiendo?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Cambiar

response.erros

 $.each(response.erros, function (key, err_values) { $('#saveform_errList').append('<li>'+err_values+'</li>'); });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!