【问题标题】:Symfony3 createFormBuilder with loop of 'add'Symfony3 createFormBuilder 带有“添加”循环
【发布时间】:2016-03-24 14:40:15
【问题描述】:

我想在 Symfony 3 中循环添加自定义表单,例如:

$defaultData = array('message' => 'Type your message here');
$profilForm = $this->createFormBuilder($defaultData)
    ->add('Nom', TextType::class);
    ->add('Description', TextType::class)

foreach ($variable as $key => $value)
{
    $profilForm
        ->add('Widget', ChoiceType::class, array(
        'choices' => array(
            'Créer' => 'C',
            'Afficher' => 'R',
            'Modifier' => 'U',
            'Supprimer' => 'D'),
        'multiple' => true,
        'expanded' => true))
}
$profilForm
    ->add('send', SubmitType::class)
    ->getForm();

问题是我得到了错误:

试图调用“Symfony\Component\Form\FormBuilder”类的名为“createView”的未定义方法。

如果我这样做:

$defaultData = array('message' => 'Type your message here');
$profilForm = $this->createFormBuilder($defaultData)
    ->add('Nom', TextType::class);
    ->add('Description', TextType::class)

foreach ($variable as $key => $value)
{
    $profilForm = $this->createFormBuilder($defaultData)
        ->add('Widget', ChoiceType::class, array(
        'choices' => array(
            'Créer' => 'C',
            'Afficher' => 'R',
            'Modifier' => 'U',
            'Supprimer' => 'D'),
        'multiple' => true,
        'expanded' => true))
}
$profilForm
    ->add('send', SubmitType::class)
    ->getForm();

它会覆盖以前的条目。

【问题讨论】:

    标签: forms symfony


    【解决方案1】:

    我让它像这样工作:

    $tmpForm = $this->createFormBuilder()
        ->add('Nom', TextType::class)
        ->add('Description', TextType::class);
    
    $i = 2;
    foreach ($listWidget as $key => $widget)
    {
        $name = preg_replace("/[^a-zA-Z0-9]/", "", $widget->getNom());
        $formBuilder = $this->get('form.factory')->createNamedBuilder($i++, FormType::class, null); 
        $formBuilder
            ->add($widget->getNom(), ChoiceType::class, array(
            'choices' => array(
                'Créer' => 'C',
                'Afficher' => 'R',
                'Modifier' => 'U',
                'Supprimer' => 'D'),
            'multiple' => true,
            'expanded' => true));
        $testForm->add($formBuilder);
    }
    $tmpForm->add('send', SubmitType::class);
    $profilForm = $tmpForm->getForm();
    

    感谢这个帖子:How to add a repeated form in a loop symfony2 for the same entity

    【讨论】:

      【解决方案2】:

      我有这个解决方案,对于“-add”上的 foreach 来说更干净。

      $form = $this->createFormBuilder();
              foreach ($activites as $activite){
                  $form = $form
                      ->add($activite->getID(). '_isOk', CheckboxType::class)
                      ->add($activite->getID(). '_commentaire', CheckboxType::class);
              }
              $form = $form->getForm();
              $form->handleRequest($request);
              return $this->render('fiche_proprete/new.html.twig',[
                  'form' => $form->createView()
              ]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-05
        • 1970-01-01
        • 1970-01-01
        • 2018-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-22
        相关资源
        最近更新 更多