38

I have created form with one element from Entity:

$promo = new Promo();

$form = $this->createFormBuilder($promo)
        ->add('code', 'text')
        ->getForm();

And I want to add file element (this field doesn't exist in the Entity). When I do:

$form = $this->createFormBuilder($promo)
        ->add('code', 'text')
        ->add('image', 'file')
        ->getForm();

I have an error: Neither property "image" nor method "getImage()". How can I add this field?

2 Answers 2

90

Use mapped:

$form = $this->createFormBuilder($promo)
    ->add('code', 'text')
    ->add('image', 'file', array(
                "mapped" => false,
            ))
    ->getForm();

In old Symfony versions (2.0 and earlier), use property_path:

$form = $this->createFormBuilder($promo)
    ->add('code', 'text')
    ->add('image', 'file', array(
                "property_path" => false,
            ))
    ->getForm();

"property_path" was removed in Symfony 2.3

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

3 Comments

Cool. Didn't know about the mapped option.
Thanks. I will use property_path = false.
It was helpful also on SF4
1

Use the property_path option:

$builder->add('image', 'file', [
    'property_path' => false,
]);

1 Comment

This is removed since Symfony 2.3. The new way is to use mapped like in the new correct answer.

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.