0

I have a problem with class form from symfony2. I want to put two forms embededd and get and set values.

Controller:

$form = $this->createFormBuilder($jurisdictionUser)
        ->add('name', 'text')
        ->add('security_user', new SecurityUserType(), array('mapped'=>false))
        ->add('email', 'text', array('required' => false, 'read_only' => true))
        ->add('roles', 'choice', array('choices' => $appModules->getRolesForJurisdictionForFormChoice(), 'multiple' => true, 'expanded' => true, 'translation_domain' => 'permissions'))
        // ->add('services', 'choice', array('multiple' => true, 'expanded' => true, 'translation_domain' => 'permissions'))
        ->add('services', null, array('multiple' => true, 'expanded' => true,
            'choices' => $services,
            'property' => 'hierarchy_name'
        ))
        ->add('save', 'submit')
        ->add('save_and_back', 'submit')
        ->getForm();

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';
}

}

How can I get and set values in my form?

2
  • Does jurisdictionUser have a link to securityUser or are they independent objects? Commented Mar 11, 2015 at 13:56
  • it is independing, so that why I put mapped = false Commented Mar 11, 2015 at 14:04

1 Answer 1

1

In order to have one form deal with two independent objects you should make yourself a container. An array will suffice:

$data = array(
    'jurisdictionUser' => $jurisdictionUser,
    'securityUser'     => $securityUser,
);
$form = $this->createFormBuilder($data)
    =>add('jurisdictionUser', new JurisdictionUserType(),
    ->add('securityUser',     new SecurityUserType(),
...
Sign up to request clarification or add additional context in comments.

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.