1
votes

I have a symfony2 main form, with a field what is a collection type of an other field, like a Product entity with multiple Tag, but these tag has a unique hash. There is an eventListener attached to the main form. If I send a data to the form and I send a Tag along with this unique has, the unique constraint on the class will say, that that field has to be unique. This work good, but in this EventListener I'm search in the DB for this unique field and if it's the right one, I do an assign, I replace the post content with an entity from the DB.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('eventDate', 'datetime',array(
            'widget' => 'single_text',
            'format' => 'yyyy-MM-dd HH:mm',
            'invalid_message' => 'Wrong datetime format. Please use like this: 2015-09-20 14:45:12',
        ))
        ->add('eventEnds', 'datetime', array(
            'widget' => 'single_text',
            'format' => 'yyyy-MM-dd HH:mm',
            'invalid_message' => 'Wrong datetime format. Please use like this: 2015-09-20 14:45:12',
        ))
        ->add('tag','collection', array(
            'type' => new TagType(),
            'allow_add' => true,
            'allow_delete' => true,
            'by_reference' => false,
            'prototype' => true,
            'property_path' => 'professional',
            'label' => false,
            'options' => array(
                'professional' => true,
            ),
        ))
        ->addEventListener(FormEvents::SUBMIT, [$this, 'submit'])
    ;
}
public function preSubmit(FormEvent $event)
{
    /** @var MainForm $data */
    $data = $event->getData();

    foreach ($data->getTags() as $tag) {
        if ($tag->getId() == null && $tag->getHash()){
            $tagDB = $this->entityManager
                ->getRepository('ApplicationBundle:Tag')
                ->findOneBy([
                    'hash' => $professional->getHash(),
                ]);

            if ($tagDB) {
                $data->removeTag($tag);
                $data->addTag($tagDB);
            }
        }
    }
    $event->setData($data);
}

If I dump the $data value after the setData, I see there the entities from the DB, but I still got a Unique validation error and I check in the validator, symfony pass in the original POST content.

Why is it like that and how can I solve this problem?

1

1 Answers

0
votes

Should be

->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'submit'])

(PRE_SUBMIT instead of SUBMIT)

Your code should be something like:

public function preSubmit(FormEvent $event)
{
    /** @var MainForm $data */
    $data = $event->getData();

    $num = count($data['tag']);
    for ($i=0;$i<$num;$i++) {
        $tag = $data['tag'][$i];
        if (!isset($tag['id']) && isset($tag['hash'])){
            $tagDB = $this->entityManager
                ->getRepository('ApplicationBundle:Tag')
                ->findOneBy([
                    'hash' => $tag['hash'],
                ]);

            if ($tagDB) {
                unset($data['tag'][$i]);
                $data['tag'][$i] = array (
                    'id' => $tagDB->getId();
                    'hash' => $tagDB->getHash();
                );
            }
        }
    }
    $event->setData($data);
}

Not sure the code is 100% correct as I have not been able to test it, but you get the idea.