I'm new to Symfony and cannot easily find Symfony 3 answers to my question.
Problem : I have a basic form setup which populates select boxes with basic id+name Entity values (Country, Company, Partner, etc.)
All the select boxes are correctly populated from the DB. However when I try saving the populated form, all the "choices" are returned as null.
Question : What am i missing to make sure the selected values in the selectboxes are sent through to the INSERT statement ?
Note : I have looked at the Form documentation but that didn't seem to cover this scenario.
Controller excerpt :
/**
* @Route("/opportunity/add", name="add_opportunity")
*/
public function addAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$opportunity = new Opportunity();
$form = $this->createForm(OpportunityType::class, $opportunity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($opportunity);
$em->flush();
return $this->redirectToRoute('/opportunity/add');
}
return $this->render(
'opportunity/add-opportunity.html.twig',
array('form' => $form->createView())
);
}
Build Form from OpportunityType :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('countries', EntityType::class, array(
'class' => 'AppBundle:Country',
'choice_label' => 'name'
))
->add('partners', EntityType::class, array(
'class' => 'AppBundle:Partner',
'choice_label' => 'name'
))
->add('companies', EntityType::class, array(
'class' => 'AppBundle:Company',
'choice_label' => 'name'
))
->add('users', EntityType::class, array(
'class' => 'AppBundle:User',
'choice_label' => 'name'
))
->add('stages', EntityType::class, array(
'class' => 'AppBundle:Stage',
'choice_label' => 'name'
))
->add('stage_changed_date', DateType::class)
->add('target_close_date', DateType::class)
->add('contract_value', TextType::class)
;
}
Form twig :
{% block body %}
<div style="max-width: 600px; margin-top:100px; padding:15px; margin-left:auto; margin-right:auto;" class="card">
{{ form_start(form) }}
<div class="form-header purple darken-4">
<h3><i class="fa"></i>Add Opportunity</h3>
</div>
{{ form_row(form.name) }}
{{ form_row(form.countries) }}
{{ form_row(form.partners) }}
{{ form_row(form.companies) }}
{{ form_row(form.users) }}
{{ form_row(form.stages) }}
{{ form_row(form.stage_changed_date) }}
{{ form_row(form.target_close_date) }}
{{ form_row(form.contract_value) }}
<button class="btn btn-raised btn-primary" type="submit">ADD</button>
{{ form_end(form) }}
</div>
{% endblock %}
Resolved, and probably not the best solution, but something which may be helpful to others.
I ended up just assigning the values as needed on the Opportunity object after Form submission before db persistence :
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Get selected values for dropdowns
$opportunity->setPartnerId($opportunity->getPartners()->getId());
$opportunity->setCountryId($opportunity->getCountries()->getId());
$opportunity->setCompanyId($opportunity->getCompanies()->getId());
$opportunity->setUserId($opportunity->getUsers()->getId());
$opportunity->setStageId($opportunity->getStages()->getId());
$em->persist($opportunity);
$em->flush();
return $this->redirectToRoute('add_opportunity');
}