I have used log statements in both of the if(isSubmitted && isValid) {}
statements, even when the second form is submitted I still get the log message from the first form.
The second form’s submit logic is never reached. If I remove the first form’s code then the second form submits no problem. It just seems to be an issue with having multiple forms in one controller.
PHP Controller
<?php
namespace WorxController;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentDependencyInjectionAttributeAutowire;
use SymfonyComponentFormExtensionCoreTypeIntegerType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyContractsHttpClientHttpClientInterface;
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 SymfonyContractsHttpClientExceptionTransportExceptionInterface
*/
#[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 File
{% 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
Answers
The two forms need to be clearly separated and this should work.
You may need to add a block prefix:
https://symfony.com/doc/current/reference/forms/types/form.html#block-prefix
This is because you are calling
createFormBuilder
with first argument asnull
for both forms.