Sruj
Sruj

Reputation: 1207

How to retrieve form field that is configured "mapped" => false in Symfony Form?

How to retrieve form field that is configured "mapped" => false and is NOT part of form ENTITY in Symfony Form?

After form submitted form object does not contain field that is configured as "mapped" => false named mainCheckbox.

Form Builder:

class DocumentCategoryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, array(
                'label' => 'Podkategoria'))
            ->add('mainCheckbox', CheckboxType::class, array(
                'label'    => 'Kategoria główna',
                "mapped" => false,
            ))
            ->add('parentId', EntityType::class, array(
                'label'    => 'Kategoria główna',
                'class' => IntraDocumentCategory::class,
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('c')
                        ->where('c.parentId = :parent')
                        ->setParameter('parent', 0);},
                'choice_label' => 'name',
            ))
            ->add('save', SubmitType::class, array('label' => 'Dodaj'));
    }

Controller:

$documentCategory = new IntraDocumentCategory();
$form = $this->createForm(DocumentCategoryType::class, $documentCategory);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    $formData = $form->getData();

While debuging $formData is an object of AppBundle\Entity\IntraDocumentCategory and contain only their fields, not mainCheckbox from form builder.

Upvotes: 7

Views: 2775

Answers (1)

René Höhle
René Höhle

Reputation: 27295

You can access the form fields with the following syntax:

$form->get('mainCheckbox')->getData();

If the field is present then you should get the value of that field.

Upvotes: 7

Related Questions