Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8158797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:43:57+00:00 2026-06-06T17:43:57+00:00

Let me first explain what’s I’m trying to do. So I have a entity

  • 0

Let me first explain what’s I’m trying to do.

So I have a entity called property that have a field called type(it can be text, email, or multi_option) and another entity called propertyValue which is the value of the property

So I found this tutorial and my question is in the EventListener instead of a simple field how can I add a select or set of checkbox having the values of other entity?

Here you have my EventListener code and you can see where I’ facing problem

    <?php
namespace Comehoy\AdBundle\Form\EventListener;

use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;

class AddValueFieldSubscriber implements EventSubscriberInterface
{
    private $factory;

    public function __construct(FormFactoryInterface $factory)
    {
        $this->factory = $factory;
    }

    public static function getSubscribedEvents()
    {
        // Tells the dispatcher that we want to listen on the form.pre_set_data
        // event and that the preSetData method should be called.
        return array(FormEvents::PRE_SET_DATA => 'preSetData');
    }

    public function preSetData(DataEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();

        // During form creation setData() is called with null as an argument
        // by the FormBuilder constructor. We're only concerned with when
        // setData is called with an actual Entity object in it (whether new,
        // or fetched with Doctrine). This if statement let's us skip right
        // over the null condition.
        if (null === $data) {
            return;
        }

        // check if the ProprertyValue object is "new"
        $type = $data->getI18nField()->getProperty()->getType();

        if ('multi_option' === substr($type, 0, 12)) {
            /*
             * 
             * Here is the problem since I'm kind of sure this is not the way to do this
             * 
             */
            $builder = $this->factory->createNamedBuilder('entity', 'value');
            $builder->add('value', 'entity', array(
                'class' => 'ComehoyAdBundle:Translation\AdPropertyOption',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('po')
                        ->orderBy('po.value', 'ASC');
                }
            ));
        } else {
            //It's not a multi option field so we use the type directly
            $form->add($this->factory->createNamed($type, 'value'));
        }
    }
}

So what I’m basically try to do is to the same thing that I can do in buildForm of a type using $bulder parameter

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:43:58+00:00Added an answer on June 6, 2026 at 5:43 pm
    public function preSetData(DataEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();
    
        $form->add($this->factory->createNamed('type', 'name', $data->getValue(), array(
                'class' => 'ComehoyAdBundle:Translation\AdPropertyOption',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('po')
                         ->orderBy('po.value', 'ASC');
                }
        )));
    }
    

    If you are using SF 2.1 then:

    public function preSetData(DataEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();
    
        $form->add($this->factory->createNamed('name', 'type', $data->getValue(), array(
                'class' => 'ComehoyAdBundle:Translation\AdPropertyOption',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('po')
                         ->orderBy('po.value', 'ASC');
                }
        )));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First let me explain. I have several addresses on the page that I put
First let me explain that I have this script that should let users enter
First let me explain my goal. I am trying to make an Animation that
First of all let me explain that I have searched for at least an
Quite new to maven here so let me explain first what I am trying
First off, let me explain that I am well aware of what this exception
Let me first explain what I'm trying to achieve using some pseudo-code (JavaScript). //
First let me explain that I've created a million lib directories scouring out all
We have a very odd problem here with SVN. Let me first explain the
Let me explain my scenario first: I have around 2000 tests to run, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.