How do I add an unbound field to a form in Symfony which is otherwise bound to an entity?

19,730

Solution 1

In your form add a text field with a false property_path:

$builder->add('extra', 'text', array('property_path' => false));

You can then access the data in your controller:

$extra = $form->get('extra')->getData();

UPDATE

The new way since Symfony 2.1 is to use the mapped option and set that to false.

->add('extra', null, array('mapped' => false))

Credits for the update info to Henrik Bjørnskov ( comment below )

Solution 2

Since Symfony 2.1, use the mapped option:

$builder->add('extra', 'text', [
    'mapped' => false,
]);

Solution 3

According to the Documentation:

allow_extra_fields

Usually, if you submit extra fields that aren't configured in your form, you'll get a "This form should not contain extra fields." validation error.

You can silence this validation error by enabling the allow_extra_fields option on the form.

mapped

If you wish the field to be ignored when reading or writing to the object, you can set the mapped option to false.

class YourOwnFormType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            array(
                'allow_extra_fields' => true
            )
        );
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $form = $builder
            ->add('extra', TextType::class, array(
                'label' => 'Extra field'
                'mapped' => false
            ))
        ;
        return $form;
    }
}
Share:
19,730

Related videos on Youtube

tetranz
Author by

tetranz

Updated on June 04, 2022

Comments

  • tetranz
    tetranz almost 2 years

    Maybe I'm missing the obvious but how do I (or can I) add an extra "unbound" field to a Symfony form that is otherwise bound to an entity?

    Let's say I have an entity with fields first_name and last_name. I do the typical thing in my form class buildForm method.

    $builder
        ->add('first_name')
        ->add('last_name')
    ;
    

    and this in my controller:

    $editForm = $this->createForm(new MyType(), $entity);
    

    That works nicely but I'd like to add another text box, let's call it "extra", and receive the value in the POST action. If I do $builder->add('extra')‍, it complains that

    NoSuchPropertyException in PropertyAccessor.php line 479:

    Neither the property "extra" nor one of the methods "getExtra()", "extra()", "isExtra()", "hasExtra()", "__get()" exist and have public access in class...

    Which is correct. I just want to use it to collect some extra info from the user and do something with it other than storing it with the entity.

    I know how to make a completely standalone form but not one that's "mixed". Is this possible?

  • tetranz
    tetranz over 11 years
    Thanks. I don't know how I missed that. It's right there in "The Book".
  • Henrik Bjørnskov
    Henrik Bjørnskov over 11 years
    The new way for Symfony 2.1 is to use the mapped option and set that to false