0

I have a form that I have built in symfony2.

$builder->add('purchaseOrder','text');
$builder->add('product', 'entity', array(
        'class' => 'WICProductBundle:Product',
        'property' => 'name',
        'query_builder' => function(EntityRepository $er) {
            return $er->createQueryBuilder('p')
                ->where('p.account=?1')
                ->orderBy('p.name', 'ASC')
                ->setParameter(1,$this->account);
        }
   ));

When I output this form in Twig and submit it, it works fine!

When I change my form builder to this and submit the form, I get an error?!?!

$builder->add('purchaseOrder','text');
$builder->add('product','text');

The first iteration of the form I fill in the purchase order field with 8 and I select an option in the product drop down with the value of 65... All works well...

The second iteration of the form I fill in the purchase order field with 8 and I fill in the product field with the value of 65... And I get this error:

Catchable Fatal Error: Argument 1 passed to WIC\PurchaseOrderLineItemBundle\Entity\PurchaseOrderLineItem::setProduct() must be an instance of WIC\ProductBundle\Entity\Product, string given, called in /Applications/MAMP/htdocs/symfonydev/vendor/symfony/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 538 and defined in /Applications/MAMP/htdocs/symfonydev/src/WIC/PurchaseOrderLineItemBundle/Entity/PurchaseOrderLineItem.php line 302

Why would my form break from a swapping out a drop down to a text box if the same value of "65" is being passed each time?

2
  • 1
    Did you clear the cache? Commented Mar 28, 2013 at 20:17
  • Yeah, its not the cache. I think it has to do with the fact that initally "query_builder" was creating an object for the product, then when I changed it to a simple text field the object no longer existed. I just dont know how to create an object out of a text field or it is even possible. Commented Mar 28, 2013 at 20:59

2 Answers 2

2

If you want to use an entity through a text field, you should add a custom model data transformer to your field which converts your "text" representation of the entity to the "real" entity & reverse transform an entity to his "text" representation.

That will avoid your model to end up with a PHP fatal error due to the setter typehint.

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

Comments

1

I couldn't find formal explanation regarded to this subject in symfony official documentation, but AFAIK any form field which represents a relation should be rendered by Entity Field Type. that's the only way symfony's basic form functionality works to render and persist a form's field that represents an entity.

Nevertheless you can use Data Transformers to achieve exactly what you wish.

from symfony cookbook:

Say you have a one-to-one relation of Task to Issue, e.g. a Task optionally has an issue linked to it. Adding a listbox with all possible issues can eventually lead to a really long listbox in which it is impossible to find something. You might want to add a textbox instead, where the user can simply enter the issue number.

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.