First, I want to clarify that I do not use Entities / Doctrine for my forms (or even). I use webservices to add / edit data fields for the database
I am working on a project where I need to edit company information with several addresses. The first address is always a physical address, so I take it in the same cycle as the company data.
However, the second (and third, or fourth, if any) address is placed in a separate database record. I can edit the company data and the company address (physical address) using one form (since all the data is in one set). For this, I need only one form, and I do not need to use a loop for the address.
Now I want to edit the second address. I look at the addresses in twig and print out all the other addresses for this company. Since I know that Symfony does not allow multiple instances of the same form, because it is associated with an identifier, etc.
The question is how should I display the form for a specific address only. which I want to edit with the click of a button (use the bootstrap to collapse to hide the form if it is not being edited)
My code is as follows:
Controller:
public function editcompany(Request $request, $klantnr)
{
[...]
$result = [
'gegevens' => [],
'addresses' => [],
];
foreach ($company as $row) {
$dataFields = [
[...]
];
foreach ($dataFields as $dataField) {
if (empty($result['gegevens'][$dataField])) {
$result['gegevens'][$dataField] = $row[$dataField];
}
}
if($row['AdrGid'] != $result['gegevens']['AdrGid']) {
$result['addresses'][$row['AdrGid']] = [
[...]
];
}
}
$form = $this->createForm(EditcompanyForm::class);
$form_adres = $this->createForm(EditcompanyAdresForm::class, array(
[...]
));
$form->handleRequest($request);
$form_adres->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
[...]
}
if($form_adres->isSubmitted() && $form_adres->isValid()) {
[...]
$data = array(
[...]
);
[...]
$this->get('http')->companyRequest($data, $admincode, $service, $token);
$request->getSession()
->getFlashBag()
->add('success', 'Linked address successfully edited');
return $this->redirect($request->getUri());
}
return $this->render('pages/company/edit.html.twig', array(
'client' => $result,
'form_company' => $form->createView(),
'form_company_adres' => $form_adres->createView()
));
}
Formbuilder (AbstractType):
class EditcompanyAdresForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$factuuradres = $options['NawFactuurAdres'];
$lands = $options['lands'];
$data = array();
$builder
[...]
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
[...]
));
}
public function getBlockPrefix()
{
return 'appbundle_form_editcompanyadresform';
}
}
Twig template (showing addresses:
{% for address in client.addresses %}
<div class="right-cont-grey">
[...]
</div>
{% endfor %}
Twig template (edit form):
{% for address in client.addresses %}
{{ form_start(form_company_adres, {' method': 'POST'}) }}
{{ form_row(form_company_adres._token) }}
<section class="collapse" id="adres_plus">
<div id="adres" class="new-adres collapse right-cont-grey">
<div class="col-sm-12">
[...]
</div>
</section>
{{ form_end(form_company_adres) }}
{% endfor %}
In short:
? , () twig ?
, .