Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 + EntityType + queryBuilder and default top value/entry

I have an entity (Category) where the user can choice a parent (always Category) for the new/edit action where the link is the vocabolaryId field.

This is my CategoryType.

class CategoryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $vocId = $options['data']->getVocabularyId();

        $builder
            ->add('name')
            ->add('vocabularyId',HiddenType::class,[
                'data' => $vocId,
            ])
            ->add('description')
            ->add('weight',null,[
                'label' => 'Posizione'
            ])
        ;
        $builder->add('parent',EntityType::class,array(
            'class' => 'AppBundle:Category',
            'query_builder' => function (EntityRepository $er) use ($vocId) {
                return $er->createQueryBuilder('c')
                    ->where('c.vocabularyId = ?1')
                    ->orderBy('c.name')
                    ->setParameter(1,$vocId);
            }
        ));
    }

To get the list of all category that have the same vocabularyId I use the "query_builder" parameter.

When I submit the form to symfony without choice a parent (or with an empty table) it replies me with : " This value is not valid." (for the parent field).

How can I set a "null" parent ? I mean a category that have no parent.

EDIT:I have added "'required' => false," like say by Stephan Vierkant, but now I have another error: "Warning: spl_object_hash() expects parameter 1 to be object, string given"

this is my controller's newAction function:

/**
     * Creates a new Category entity.
     *
     * @Route("/new", name="admin_category_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request, $vocId)
    {

        $category = new Category($vocId);
        $form = $this->createForm('AppBundle\Form\CategoryType', $category);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            dump($category);
            exit();

            $em->persist($category);
            $em->flush();

            return $this->redirectToRoute('admin_category_show', array('vocId' => $vocId, 'id' => $category->getId()));
        }

        return $this->render('category/new.html.twig', array(
            'category' => $category,
            'form' => $form->createView(),
            'vocId' => $vocId
        ));
    }
like image 277
ZioBudda Avatar asked Dec 14 '25 20:12

ZioBudda


1 Answers

Set required (docs) to false:

$builder->add('parent',EntityType::class,array(
            'class' => 'AppBundle:Category',
            'required' => false,
            'query_builder' => function (EntityRepository $er) use ($vocId) {
                return $er->createQueryBuilder('c')
                    ->where('c.vocabularyId = ?1')
                    ->orderBy('c.name')
                    ->setParameter(1,$vocId);
            }
        ));

You can set a placeholder (docs) if you don't want an empty option.

like image 50
Stephan Vierkant Avatar answered Dec 18 '25 18:12

Stephan Vierkant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!