php - Symfony - form builder : ChoiceType always NULL after submit
Get the solution ↓↓↓I work with symfony and I have a form with a ChoiceType, the choices are coming as array from a fields from an other entity
FormType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('paq', ChoiceType::class, array(
'choices' => $options['data_array'],
));
}
Twig
<form action="{{ path('ligne_panier_create', { 'id': opProd.produit.id }) }}" method="post" {{ form_enctype(form) }} >
{{form_start(form)}}
<div class="form-group">
<h5>Choisir le type de paquetage</h5>
{{form_widget(form.paq, { 'attr': {'class': 'form-control subject'} }) }}<br>
</div>
<p class="text-center">
<button type="submit" id="_submit" name="_submit" value="" class="btn btn-primary" ><i class="fa fa-sign-in"></i>Ajouter </button>
</p>
{{form_end(form)}}
</form>
Controller
public function createAction(Request $request, $id)
{
$em=$this->getDoctrine()->getManager();
$Lpanier = new LignePanier();
$produit = $em->getRepository('CentralBundle:Produit')->find($id);
$opProd = $em->getRepository('CentralBundle:OperationProduit')->find($id);
$data_array = $opProd->getPaquetage();
$form = $this->createForm('Central\CentralBundle\Form\LignePanierType', $Lpanier, array('data_array'=>$data_array));
$form->handleRequest($request);
$paq= $form['paq']->getData(); // always null !!
$qte= $form['qte']->getData(); // work fine
$Lpanier->setPrixTotProd($qte*$produit->getPrixVenteTTC());
$em->persist($Lpanier);
$em->flush($Lpanier);
return $this->render('CentralBundle:Default:commitSuccess.html.twig');
}
the list Choice display perfectly , but the problem that after submit the value of this field always get NULL in database.
I have other input in the form and they submitted perfectly!
Someone can help me please ?
Answer
Solution:
In your template you have opProd { 'id': opProd.produit.id } object, but in your render method you don't set this var.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: apache/2.4.52 (win64) openssl/1.1.1m php/8.1.2 server at localhost port 80
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.