In my Symfony2, I have a table of a user's emails (entities in my database):
{% for email in emails %}
...
{{ email.subject }}
...
{% endfor %}
I would like to make these selectable by wrapping the table in a form and adding a checkbox to each of these rows.
What's the best way to approach this in Symfony2? I can only think that I'll have to create a Type inside a Type inside a Type, which seems less than ideal:
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';
}
}