[英]Render a field from a collectionType form in twig with symfony 4
I want to use a form_widget to render a field for a collectionType form. 我想使用form_widget来呈现collectionType表单的字段。 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".
属性“名字”或方法“名字()”,“ getfirstname()” /“ isfirstname()” /“ hasfirstname()”或“ __call()”都不存在,并且在类“ Symfony \\”中没有公共访问权限组件\\表格\\ 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,
]);
}
}
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: 并将其添加到我的控制器的form方法中:
'allow_add' => true
You might want to add the code of the entity to your question: Tickets.php 您可能需要将实体的代码添加到您的问题中:Tickets.php
In Tickets.php you probably define the class Tickets and in that class you probably don't have any of the methods listed in the error message. 在Tickets.php中,您可能定义了Tickets类,并且在该类中,您可能没有错误消息中列出的任何方法。 Adding this method with the exact name should help:
添加具有确切名称的此方法应该会有所帮助:
public function getfirstname() { return $this->firstname; }
About the twig code: you might want to add some more to the question. 关于细枝代码:您可能想在问题中添加更多内容。 For example, do you have
{{ form_start(form) }}
or something else in the beginning? 例如,您
{{ form_start(form) }}
是否有{{ form_start(form) }}
或其他内容?
Then as it is a CollectionType, you probably want to render some input field for each of the members in the collection. 然后,由于它是一个CollectionType,您可能想要为集合中的每个成员呈现一些输入字段。 Maybe something like this:
也许是这样的:
{% for ticket in form.tickets %}
<div class="ticket">{{ form_widget(ticket.firstname) }}</div>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.