在我的Symfony2中,我有一个用户电子邮件表(我数据库中的实体):
{% for email in emails %}
...
{{ email.subject }}
...
{% endfor %}
我想通过将表格包装在表格中并为每个行添加一个复选框来使这些可选择。
在Symfony2中解决此问题的最佳方法是什么?我只能认为我必须在Type内部的Type中创建一个Type,这似乎不太理想:
class SelectableEmailsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('selectableEmails', 'collection', [ 'type' => new SelectableEmailType() ]);
}
public function getName()
{
return 'selectableEmails';
}
}
class SelectableEmailType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', new EmailType());
$builder->add('selected', 'checkbox');
}
public function getName()
{
return 'selectableEmail';
}
}
class EmailType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('subject', 'text');
...
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => 'EmailOctopus\Bundle\ListsBundle\Entity\ListEntity',
]);
}
public function getName()
{
return 'email';
}
}
答案 0 :(得分:1)
这是回答有约束力的问题。
// Query a list of emails
$emails = $emailRepository->findAll();
// Turn them into selectable emails
$selectableEmails = array();
foreach($emails as $email)
{
$selectableEmails[] = array('email' => $email, 'selected' => false);
}
// Pass this to the root form
$formData = array('selectableEmails' => $selectableEmails);
处理完成后的表格后,您需要提取可选择的电子邮件列表,并通过它来获取实际选择的列表。
这只是一种方法。它适用于向实体添加其他表单相关属性而无需更改实体本身。
答案 1 :(得分:0)
我最终分配了id。使用此解决方案将每封电子邮件发送到复选框:
Build a form having a checkbox for each entity in a doctrine collection