我在两个if(isSubmitted && isValid) {}
语句中都使用了log语句,即使提交了第二个表单,我仍然会从第一个表单中获得日志消息。
永远不会到达第二个窗体的提交逻辑。如果我删除第一个表单的代码,那么第二个表单提交没有问题。这似乎只是一个问题,在一个控制器中有多个窗体。
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,
]);
}
}
小枝文件
{% 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 %}
2条答案
按热度按时间7ivaypg91#
这两种形式需要明确分开,这应该是可行的。
mznpcxlj2#
您可能需要添加块前缀:https://symfony.com/doc/current/reference/forms/types/form.html#block-prefix
这是因为对于两种形式,您都将第一个参数作为
null
调用createFormBuilder
。