Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form type mapped false symfony2

Tags:

forms

php

symfony

I have a trouble with a formtype with mapped=false.

In controller, I called the form with:

$form = $this->createForm(new JurisdictionUserNewType(), $jurisdiction_user);

This is my JurisdictionUserNewType:

class JurisdictionUserNewType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $transformer = new CapitalLetterToLowerCaseTransformer();

        $builder
            ->add('name', 'text')
            ->add($builder->create('email', 'email')
                           ->addModelTransformer($transformer))
            ->add('securityUser', new SecurityUserType(), array('mapped' => false))

            ->add('save', 'submit');

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
           'data_class' => 'Radmas\Open010Bundle\Document\JurisdictionUser'
        ));
    }

    public function getName()
    {
        return 'jurisdictionUserNew';
    }
} 

This is my SecurityUserType :

class SecurityUserType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('first_name', null, ['label' => 'profile.edit.labels.first_name', 'icon_class' => 'fa fa-user'])
            ->add('last_name', null, ['label' => 'profile.edit.labels.last_name', 'icon_class' => 'fa fa-user'])
            ->add('nickname', null, ['label' => 'profile.edit.labels.nickname',
                'attr' => [ 'help_text' => 'profile.edit.labels.nickname_help'], 'icon_class' => 'fa fa-globe']);
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
           'data_class' => 'Radmas\SecurityBundle\Document\SecurityUser'
        ));
    }

    public function getName()
    {
        return 'securityUser';
    }
}

When I put data in the form, I get the object jurisdictionUser in the modalview but I dont get object securityUser.

like image 622
Carlos Sierra Avatar asked Jan 09 '23 12:01

Carlos Sierra


1 Answers

If you set 'mapped' => false on any field, you're saying that that field isn't related to your entity, so you don't get it when you retrieve the entity from the submitted form.

You can get it anyway as a single field from the form, as:

$form->handleRequest($request); 
if ($form->isValid()) { 
    $entity = $form->getData(); 
    $securityUser = $form->get('securityUser')->getData();    
}
like image 63
Alessandro Lai Avatar answered Jan 11 '23 02:01

Alessandro Lai