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 8340829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:17:20+00:00 2026-06-09T05:17:20+00:00

I am doing something very similar to this cookbook example http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html#adding-an-event-subscriber-to-a-form-class The main difference

  • 0

I am doing something very similar to this cookbook example http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html#adding-an-event-subscriber-to-a-form-class

The main difference is that my field type is an entity and not a text type.

So my field subscriber preSetData method looks like this:

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

    if (null === $data) {
        return;
    }

    if(!$data->getIsCategorized()){

        $form->add(

            $this->factory->createNamed('categories', 'entity', array(
            'class' => 'My\PostBundle\Entity\Category',
            'property'     => 'name',
            'multiple'     => true,
            'label' => 'Category'
            )
            )
        );
    }
}

This is giving the following error

Class does not exist
500 Internal Server Error - ReflectionException 

If I add the entity directly in my form type with $builder->add('categories, 'entity', array(... it works fine

Is it possible to attach an entity field type to a form using a field event subscriber in this fashion?

  • 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-09T05:17:23+00:00Added an answer on June 9, 2026 at 5:17 am

    I ran into the same problem, and actually it’s because the factory->createNamed() method has more argument than the builder->add
    The third argument isn’t the options array, but a “data” argument.

    So here’s what you should do :

        $form->add(
    
            $this->factory->createNamed('categories', 'entity', null, array(
            'class' => 'My\PostBundle\Entity\Category',
            'property'     => 'name',
            'multiple'     => true,
            'label' => 'Category'
            )
            )
        );
    

    (add null before the options array)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to accomplish something very similar to what this user was doing Here
I'm doing something very similar to the stackoverflow question preview only much more basic.
This is a very simple issue. I must just be doing something stupid: This
I must be doing something stupid here, because I have some very similar code
I want to do something very similar to this but in the CakePHP world
I'm facing a apparently very strange problem (I must be doing something wrong, just
I'm doing a project (something like a restaurant) for collage, and I'm not very
My problem is very similar to this question , however I am posting a
How would I design the tables for a matching system doing something like this:
I'm trying to do something very similar to what thomas.mery posted, but can't make

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.