dongxian1921 2018-10-17 14:26
浏览 50
已采纳

使用symfony 4从twig中的collectionType表单渲染字段

I want to use a form_widget to render a field for a collectionType form. Here is my controller :

/**
 * @Route("/ticket", name="ticket")
 */
public function ticket(Request $request)
{
    $data = $request->getSession()->get('orders');
    $number = $data->getNumberOfTickets();
    for ($i=1; $i<=$number ;$i++){
    $tickets[] = new Tickets();
    }
    $form = $this->createForm(CollectionType::class, $tickets, ['entry_type' => TicketsType::class] );
    $form->handleRequest($request);
    dump($request);

    return $this->render('louvre/ticket.html.twig', [
        'tickets' =>$tickets,
        'form' => $form->createView()
    ]);
}

and when i try :

{{ form_widget(tickets.firstname)}}

or

{{ form_widget(form.firstname)}}

or

{{ form_widget(form.tickets.firstname)}}

I have an error :

Neither the property "firstname" nor one of the methods "firstname()", "getfirstname()"/"isfirstname()"/"hasfirstname()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView".

Here is my form :

class TicketsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('category', CheckboxType::class, [
                'attr' => [
                    'class' => 'form-control'
                ]
            ])
            ->add('firstname', TextType::class, [
                'attr' => [
                    'class' => 'form-control'
                ]
            ])
            ->add('lastname', TextType::class, [
                'attr' => [
                    'class' => 'form-control'
                ]
            ])
            ->add('country', TextType::class, [
                'attr' => [
                    'class' => 'form-control'
                ]
            ])
            ->add('dateOfBirth', DateType::class, [
                'attr' => [
                    'class' => 'form-control'
                ],
                'widget' => 'single_text',
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Tickets::class,
        ]);
    }
}
  • 写回答

2条回答 默认 最新

  • dongwen4630 2018-10-17 15:56
    关注

    To render a field I just need to use the prototype in twig:

    {{ form_widget(form.vars.prototype.firstname) }}
    

    and add this in my form method, in my controller:

    'allow_add' => true
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度