2

I'm building simple CMS in S2 and I have little trouble wih processing relational Category in Article form.

Code for CategoryType and action: http://codepaste.net/ahyoig all like in tutorial/manual on Symfony site.

Form looks fine, but when i process data (also like in manual) i get error: "Expected argument of type array, string given" in select field and I ahve no idea right now how to solve it.

2 Answers 2

2

I think your problem is because you set the option multiple to false. This means that the form type will return a string value (since you set multiple to false, a single value) but your relation is probably setup as a many-to-many relation.

Setting the multiple option to true will send an array instead of a string to your object which is what is expected by the application. Then, if you don't like the listbox look of your CategoryType in HTML, you can render it differently using form theming in Symfony2.

Hope this helps, Matt

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need a CategoryType. The creation of $categories array is also useless.

The best choice is to use the native entity form type:

<?php

use Doctrine\ORM\EntityRepository;

//...

$article = new Article();

$form = $this->get('form.factory')
            ->createBuilder('form',$article)
            ->add('category', 'entity', array
            (
                'class' => 'CMSBackBundle:Category',
                'query_builder' => function(EntityRepository $er)
                {
                    return $er->createQueryBuilder('c')
                            ->orderBy('c.title', 'ASC');
                },
                'expanded' => false,
                'multiple' => false
            )
            // ...
            ->getForm();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.