类表格嵌入symfony2.3

时间:2015-03-11 13:31:20

标签: forms symfony

我的symfony2类表格有问题。我想把两个表格嵌入并获取和设置值。

控制器:

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

}

如何在表单中获取和设置值?

1 个答案:

答案 0 :(得分:1)

为了让一个表单处理两个独立的对象,你应该把自己变成一个容器。数组就足够了:

$data = array(
    'jurisdictionUser' => $jurisdictionUser,
    'securityUser'     => $securityUser,
);
$form = $this->createFormBuilder($data)
    =>add('jurisdictionUser', new JurisdictionUserType(),
    ->add('securityUser',     new SecurityUserType(),
...