I'm trying to figure out how I can use more custom form field names using Symfony's form builder functionality while still being able to use components like the Validator
and CSRF protection.
For example, Symfony automatically sets field names to the format form_name[form_field]
, while I want to set it as something like form_name[$var1][labels][$var2]
.
This is very easy to do using plain HTML forms but I haven't found a simple way to do it using the form builder.
This is what I have right now:
// ...
foreach($session->get("agreement_form_data")->areasOfWork as $area) {
foreach($this->get("service.label_manager")->getLabelsByArea($area) as $label) {
$form->add(StringFunctions::toHtmlId($label->getLabelName()), TextType::class, [
"constraints" => [
new Assert\NotBlank()
]
]);
}
$form->add("create_agreement", SubmitType::class, [
"label" => "Create Agreement"
]);
}
// ...
return $this->render("create-agreement.html.twig", [
"form" => $form->createView()
]);
HTML/Twig:
{% extends "base.html.twig" %}
{% block page_title %}Create Agreement - Page 2{% endblock %}
{% block body %}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endblock %}
How would I return the form values as a more complex array?