0

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';
    }
}
Jonathan
  • 13,947
  • 17
  • 94
  • 123
  • That's pretty much what it takes especially if EmailType is used in other forms. This kind of nesting is quite common. Of course if all you really need to do is to select emails (and not edit the subject) then the EmailType is not needed. – Cerad Apr 26 '14 at 13:47
  • @Cerad Thanks, that's good to know. I do just need to select emails - if I remove the `EmailType`, how should I bind the entity? – Jonathan Apr 26 '14 at 14:33
  • You don't really need to bind the email entity since you are not modifying it.. Instead, you can access it directly from a twig template using the vars parameter. {{ selectableEmail.vars.email.subject }} – Cerad Apr 26 '14 at 15:08
  • @Cerad But without binding at least the id, how will I know what emails have been selected? – Jonathan Apr 26 '14 at 15:12

2 Answers2

1

This is answering the binding question.

// 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);

After processing a posted form you would pull out the list of selectableEmails and run through it to get the list actually selected.

This is just one approach. It works well for adding additional form related attributes to an entity without changing the entity itself.

Cerad
  • 48,157
  • 8
  • 90
  • 92
0

I ended up assigning the id. of each email to a checkbox, using this solution:

Build a form having a checkbox for each entity in a doctrine collection

Community
  • 1
  • 1
Jonathan
  • 13,947
  • 17
  • 94
  • 123