smugford
smugford

Reputation: 769

Dynamic Forms Symfony2 PRE_SUBMIT Works for update but does not submit

I am working on getting a dynamic form to join the product to a category table.

The Games entity has a Game Categories Related Back to it.

 /**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\GameCategories", inversedBy="products")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="category_id")
 */
public $category_id;    



/**
 * Set categoryId
 *
 * @param \AppBundle\Entity\GameCategories $categoryId
 *
 * @return Products
 */
public function setCategoryId(\AppBundle\Entity\GameCategories $categoryId = null)
{
    $this->category_id = $categoryId;

    return $this;
}

/**
 * Get categoryId
 *
 * @return \AppBundle\Entity\GameCategories
 */
public function getCategoryId()
{
    return $this->category_id;
}

Then the event listener

  $builder->addEventListener(
        FormEvents::PRE_SUBMIT,
        function (FormEvent $event) {
             $product = $event->getData();
             $form = $event->getForm();

            // $game_id = $product->getGameId(); 
            $game_id = $product['game_id'];

            $formOptions = array(
                'class' => 'AppBundle\Entity\Products',
                'property' => 'category_id',
                'query_builder' => function (EntityRepository $er) use ($game_id) {

                     $query = $er->createQueryBuilder('i')
                                ->select(array('i'))
                                ->where('i.game_id = :game_id')
                                ->setParameter('game_id', $game_id)
                                ->orderBy('i.name', 'ASC');
                        return $query;
                },
            );

           $form->add('category_id', EntityType::class, $formOptions);
        }
    );       

When I do the onclicks it works as expected and the proper select box updates.

But when I actually submit the form I get the following error.

"Expected argument of type "AppBundle\Entity\GameCategories", "AppBundle\Entity\Products" given"

PRE_SUBMIT looks like it is using the Products which is what I am trying to get it to do. but when it actually submits it's looking for the other entity.

Does anyone have any ideas on getting around this or a better idea on how to implement a chained dropdown sequence these days.

Thanks in advance.

EDIT ONE

I was using the wrong Entity in the query builder.

I was able to get the dynamic form to change but it only shows the integer...

choose category... 1 2 3

instead of choose category... Category 1 Category 2 Category 3

 // Category ID

            $formOptions = array(
                'class' => 'AppBundle\Entity\GameCategories',
                'label' => 'Game Category',
                'property' => 'category_id',
                'placeholder' => 'Choose a Category...',
                'query_builder' => function (EntityRepository $er) use ($game_id) {

                    $query = $er->createQueryBuilder('i')
                                ->select(array('i'))
                                ->where('i.gameId = :game_id')
                                ->setParameter('game_id', $game_id)
                                ->orderBy('i.name', 'ASC');

                    return $query;

                },
            );
            // create the field, this is similar the $builder->add()
            // field name, field type, data, options
           $form->add('category_id', EntityType::class, $formOptions);

How would i get the names instead of the integer?

EDIT TWO

This is what ended up working.

 $formOptions = array(
                'class' => 'AppBundle\Entity\GameCategories',
                'label' => 'Game Category',
                'property' => 'category_id',
                'choice_label' => function ($category) {
                    return $category->getName();
                },
                'placeholder' => 'Choose a Category...',
                'query_builder' => function (EntityRepository $er) use ($game_id) {
                    $query = $er->createQueryBuilder('i')
                                ->select(array('i'))
                                ->where('i.gameId = :game_id')
                                ->setParameter('game_id', $game_id)
                                ->orderBy('i.name', 'ASC');
                    return $query;
                },
            );

Upvotes: 1

Views: 221

Answers (1)

You are adding to your form a field category_id that contains a GameCategories, but you are configuring it with a list of Products, because you set 'class' => 'AppBundle\Entity\Products'. You should set the class to AppBundle\Entity\GameCategories, to get a list of categories.

EDIT

To choose which property is used to display your entities in the list, use the choise_label option:

choice_label
type: string, callable or PropertyPath

This is the property that should be used for displaying the entities as text in the HTML element

Upvotes: 1

Related Questions