Symfony双表单仅首个可提交问题求助
问题:同一控制器下多表单提交异常
我在两个表单的if(isSubmitted && isValid)代码块中都添加了日志语句,但提交第二个表单时,仍只会触发第一个表单的日志,第二个表单的提交逻辑从未执行。移除第一个表单的代码后,第二个表单可正常提交,问题仅在同一控制器中存在多个表单时出现。
PHP控制器代码
<?php namespace Worx\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Form\Extension\Core\Type\IntegerType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Contracts\HttpClient\HttpClientInterface; class BCBusinessEntityMappingController extends AbstractController { public HttpClientInterface $client; private \mysqli|false $database; public function __construct(HttpClientInterface $client, #[Autowire('%env(DBHOST)%')] private $dbhost, #[Autowire('%env(DBUSER)%')] private $dbuser, #[Autowire('%env(DBPASS)%')] private $dbpass, #[Autowire('%env(DBNAME)%')] private $dbname) { $this->client = $client; $this->database = mysqli_connect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname, 3306); } /** * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface */ #[Route('/test', name: 'test')] public function load(Request $request): Response { // Grab our store context from the session. $session = $request->getSession(); $context = $session->get('store_context'); // See if we already have the seller saved. If so we will show this // as the default value for the form. $stmt = $this->database->prepare("SELECT SELLER STATEMENT"); $stmt->bind_param('s', $context); $stmt->execute(); $results = $stmt->get_result(); $seller = ''; if ($results->num_rows > 0) { $seller = $results->fetch_object()->seller; } $seller_form = $this->createFormBuilder(null, ['attr' => ['id' => 'seller-form']]) ->add('seller', TextType::class, ['label' => 'Seller Entity', 'data' => $seller]) ->add('submit1', SubmitType::class, ['label' => 'Submit']) ->getForm(); $seller_form->handleRequest($request); if ($seller_form->isSubmitted() && $seller_form->isValid()) { $seller_form_data = $seller_form->getData(); if ($results->num_rows > 0) { // Update existing record. $stmt = $this->database->prepare("UPDATE STATEMENT"); } else { // Insert new record. $stmt = $this->database->prepare("INSERT INTO STATEMENT"); } $stmt->bind_param('ss', $context, $seller_form_data['seller']); $stmt->execute(); return $this->redirect($request->getUri()); } $buyer_form = $this->createFormBuilder(null, ['attr' => ['id' => 'business-form']]) ->add('customer_id', IntegerType::class, ['label' => 'Customer Id']) ->add('business_entity', TextType::class, ['label' => 'Business Entity']) ->add('submit2', SubmitType::class, ['label' => 'Submit']) ->getForm(); $buyer_form->handleRequest($request); if ($buyer_form->isSubmitted() && $buyer_form->isValid()) { $buyer_form_data = $buyer_form->getData(); $stmt = $this->database->prepare("INSERT INTO STATEMENT"); $stmt->bind_param("sis", $context,$buyer_form_data['customer_id'], $buyer_form_data['business_entity']); if (TRUE === $stmt->execute()) { return $this->redirect($request->getUri()); } else { // todo - return a message var for twig that will say an error. // Look into using $stmt->error; This should be a string containing // the error message. } } // Get the current group mapping data. $stmt = $this->database->prepare("SELECT id, customer_id, business_entity FROM business_entity_mapping WHERE context=?"); $stmt->bind_param('s', $context); $stmt->execute(); $results = $stmt->get_result(); $rows = ''; if ($results->num_rows > 0) { $rows = $results->fetch_all(MYSQLI_ASSOC); } return $this->render('business_entity_mapping_form.html.twig', [ 'seller_form' => $seller_form->createView(), 'business_entity_mapping_form' => $buyer_form->createView(), 'rows' => $rows, ]); } }
Twig模板代码
{% extends "base.html.twig" %} {% block stylesheets %} {{ parent() }} <link href="{{ asset('css/table.css') }}" rel="stylesheet"/> <link href="{{ asset('css/business_entity.css') }}" rel="stylesheet"/> {% endblock %} {% block body %} {% if seller_form is not empty %} <div class="seller-form"> {{ form(seller_form) }} </div> {% endif %} {% if business_entity_mapping_form is not empty %} <div class="business-entity-mapping-form"> {{ form(business_entity_mapping_form) }} </div> {% endif %} <table class="styled-table"> <thead> <tr> <th>Customer ID</th> <th>Business Entity</th> <th></th> <th></th> </tr> </thead> <tbody> {% if rows is not empty %} {% for row in rows %} <tr> <td> <div class="cell-border">{{ row.customer_id }}</div> </td> <td> <div class="cell-border">{{ row.business_entity }}</div> </td> <td> <div class="cell-border center-text"> <a href="{{ path('bc_business_entity_edit', {'id': row.id}) }}">edit</a> </div> </td> <td> <div class="center-text"> <a href="{{ path('bc_business_entity_delete', {'id': row.id}) }}">delete</a> </div> </td> </tr> {% endfor %} {% else %} <tr> <td>No Records Found!</td> </tr> {% endif %} </tbody> </table> {% endblock %}
内容的提问来源于stack exchange,提问作者FatGuyLaughing
相关产品推荐
相关产品推荐

