Symfony2 - Entire child entity in form and Twig template

空扰寡人 提交于 2019-12-06 03:31:50

So I googled it a lot and here's what worked for me (Symfony 2.2)
Updated as @flu suggested in a comment

{% for key, child in form %}
<div>
    {% set entity = form.vars.choices[key].data %}
    {{ form_widget(child) }}
    {{ form_label(child) }}
</div>
{% endfor %}

and entity is the child object. As of Symfony 2.3 there will be just form.vars.data, if I understand correctly: https://github.com/symfony/symfony/pull/5023

I´ve spend the whole day to get a solution for this problem.

First I created a new FormType which extends the ChoiceType:

class NomineesType extends AbstractType
{
protected $em;

public function __construct(EntityManager $em) {
    $this->em = $em;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $submissionRepository = $this->em->getRepository('BundleName:Submission');
    $choices = $submissionRepository->findBy(array(
        'nominee' => true
    ));
    $resolver->setDefaults(array(
        'choices' => $choices
    ));
}

public function getName()
{
    return 'nominees';
}

public function getParent()
{
    return 'choice';
}
}

Than I had to register the service:

services:
form.type.nominees:
    class: Bundle\Form\Type\NomineesType
    arguments:
        entityManager: "@doctrine.orm.entity_manager"
    tags:
        - { name: form.type, alias: nominees }

After that I added the new type to my form:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $game = $this->game;
    $builder->add('submission', 'nominees', array(
        'expanded' => true,
        'multiple' => false
    ));
    $builder->add('email');
}

Now I am able to get all properties in my twig-template:

{% block nominees_widget %}
{% spaceless %}
    {% if expanded %}
        <ul {{ block('widget_container_attributes') }}>
        {% for child in form %}
            <li>
                {{ child.get('form').get('label').firstname }}
                {{ child.get('form').get('label').lastname }}
                {{ form_widget(child) }}
                {{ form_label(child) }}
            </li>
        {% endfor %}
        </ul>
    {% else %}
        {# just let the choice widget render the select tag #}
        {{ block('choice_widget') }}
    {% endif %}
{% endspaceless %}
{% endblock %}

Because the choice list is returning only integer values I´ve created a new data transformer to transform the id of the choice into an entity:

class IntToEntityTransformer implements DataTransformerInterface
{
/**
 * @var ObjectManager
 */
private $om;

/**
 * @param ObjectManager $om
 */
public function __construct(ObjectManager $om)
{
    $this->om = $om;
}

/**
 * Transforms an object (issue) to a string (number).
 *
 * @param  Issue|null $issue
 * @return string
 */
public function transform($issue)
{
    if (null === $issue) {
        return "";
    }

    return $issue->getNumber();
}

/**
 * Transforms a string (number) to an object (issue).
 *
 * @param  string $number
 * @return Issue|null
 * @throws TransformationFailedException if object (issue) is not found.
 */
public function reverseTransform($number)
{
    if (!$number) {
        return null;
    }

    $issue = $this->om
        ->getRepository('BundleName:Submission')
        ->findOneBy(array('id' => $number))
    ;

    if (null === $issue) {
        throw new TransformationFailedException(sprintf(
            'An Submission with number "%s" does not exist!',
            $number
        ));
    }

    return $issue;
}
}

This data transformer is initialized in the build form method:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $transformer = new IntToEntityTransformer($this->em);
    $builder->add($builder->create('submission', 'nominees', array(
        'expanded' => true,
        'multiple' => false
    ))->addModelTransformer($transformer));
    $builder->add('email');
}

I've been fighting this problem with Symfony 2.3. Don't know how it works in 2.1.

In my case I had a entity choice (expanded and multiple) for which I needed to add the entitie's 'descripcion' field, next to the label of each choice.

I solved as follows:

1- created a custom type, whose parent is EntityType, and name 'xxxxxxx_form_reparacionnormalizada'

class ReparacionNormalizadaType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'class'        => 'xxxxxxxxx\Entity\TipoReparacion',
        ));
    }

    public function getName()
    {
        return 'xxxxxxx_form_reparacionnormalizada';
    }

    public function getParent()
    {
        return 'entity';
    }
}

2- register the form type as service as usual

3- created a custom template as follows:

{% block xxxxxxx_form_reparacionnormalizada_widget %}
<ul {{ block('widget_container_attributes_choice_widget') }}>

    {% for id, child in form.children %}

    <li>
        {{ form_label(child, child.vars.label|default(null), { 'in_list_checkbox' : true, 'widget' : form_widget(child) } ) }}

        <span>{{ choices[id].data.descripcion }}</span>

    </li>
    {% endfor %}
</ul>
{% endblock %}

4- in your sonata admin you use your custom type instead of 'entity'

hope it helps

regards

javier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!