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

188
Views
El formulario Symfony AJAX devuelve un objeto vacío

Estoy tratando de crear un formulario AJAX con Symfony, pero mi formulario devuelve un objeto vacío. Cuando envío texto escrito manualmente o una matriz, todo funciona bien. ¿Dónde está el bicho? ¿Hago algo mal con el formulario o el código javascript es el problema?

 /** * Renders the "new" form * * @Route("/", name="demo_new") * @Method("GET") */ public function newAction(Request $request) { $entity = new Demo(); $form = $this->createForm(DemoType::class, $entity); return $this->render('default/new.html.twig', array( 'entity' => $entity, 'form' => $form->createView() ) ); } /** * * @Route("/", name="demo_create") * @Method("POST") * */ public function createAction(Request $request) { if (!$request->isXmlHttpRequest()) { return new JsonResponse(array('message' => 'You can access this only using Ajax!'), 400); } $entity = new Demo(); $form = $this->createForm(DemoType::class, $entity, array( 'action' => $this->generateUrl('demo_create'), 'method' => 'POST', )); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $form->getData(); return new JsonResponse( [ 'message' => 'Success!', 'data' => $data ], 200); } $response = new JsonResponse( array( 'message' => 'Error', 'form' => $this->renderView('default/new.html.twig', array( 'entity' => $entity, 'form' => $form->createView(), ))), 400); return $response; } }

y código Javascript:

 function initAjaxForm() { $('body').on('submit', '.ajaxForm', function (e) { e.preventDefault(); $.ajax({ type: $(this).attr('method'), url: $(this).attr('action'), data: $(this).serialize() }) .done(function (data) { if (typeof data.message !== 'undefined') { console.log(data.data); console.log(data.message); } }) .fail(function (jqXHR, textStatus, errorThrown) { if (typeof jqXHR.responseJSON !== 'undefined') { if (jqXHR.responseJSON.hasOwnProperty('form')) { $('#form_body').html(jqXHR.responseJSON.form); } $('.form_error').html(jqXHR.responseJSON.message); } else { alert(errorThrown); } }); }); }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Tuve el mismo problema hoy con la versión 2.8. Dejaré esto aquí en caso de que termine curando a alguien más. He agregado esto a mi creador de formularios.

 /** * {@inheritdoc} */ public function getBlockPrefix() { return ''; }
about 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!