duankang5882 2015-02-26 09:40
浏览 24

使用与同一个类相关的一些动态表单

Symfony 2.3

I'm embedding some Forms to be able to change each user property related to permissions. I've created an UserAdminType which is displayed for each user in the same page:

<?php

namespace Msalsas\UserAdminBundle\Form;

use Msalsas\UserBundle\Entity\User;
use Msalsas\UserBundle\Entity\ExtendedUser;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;

class UserAdminType extends AbstractType
{
private $user;

public function __construct(User $user)
{
    $this->user = $user;
    $this->extendedUser = new ExtendedUser($this->user);
}

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{

    $extendedUser = $this->extendedUser;

    $builder
        ->add('extendedRole',
            'choice', array('choices' => array(
                $extendedUser::ROLE_1 => "Role 1",
                $extendedUser::ROLE_2 => "Role 2",
                $extendedUser::ROLE_3 => "Role 3",
             ),
            'label' => $this->user->getUsername()
        ))
        ->add('Change roles for '.$this->user->getUsername(), 'submit')
    ;

    $builder->addEventListener(
        FormEvents::POST_SUBMIT,
        function (FormEvent $event) {
            $form = $event->getForm();

            $data = $event->getData();

            if( ! $form->getClickedButton('Change roles for '.$this->user->getUsername()) )
            {
                  // Here I should avoid submitting the form 
            }

        }
    );

}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Msalsas\UserBundle\Entity\ExtendedUser',
        'empty_data' => function (FormInterface $form) {
                return null;
            }
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'extendedUserRoleForm';
}
}

The problem is that when I submit one of those forms, all other forms are also submitted, returning an error, because the extendedUser uses a constructor to initialize the object with the User as parameter:

Catchable Fatal Error: Argument 1 passed to Msalsas\UserBundle\Entity\ExtendedUser::__construct() must be an instance of Msalsas\UserBundle\Entity\User, none given, called in .../vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Type/FormType.php on line 140 and defined

I've also tried to set the empty_data with a new default ExtendedUser:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Msalsas\UserBundle\Entity\ExtendedUser',
        'empty_data' => function (FormInterface $form) {
                return $this->extendedUser;                    
            }
    ));
}

Now, when submitting the form, the new entity is persisted, but the other forms are still submitted, and returning an error: This form should not contain extra fields. This seems to be due to the duplicated property name (extendedRole).

How could I avoid the other forms to be submitted?

  • 写回答

1条回答 默认 最新

  • douyicao2199 2015-02-26 11:28
    关注

    I've found out the solution here.

    Each Form must have different name. So I've added a $name property, and assigned it in the constructor:

    private $user;
    private $name = 'default_name_';
    private $extendedUser;
    
    public function __construct(User $user, $formName)
    {
        $this->user = $user;
        $this->extendedUser = new ExtendedUser($this->user);
        $this->name = $this->name.$formName;
    }
    
    //... (No event required)
    
    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
    

    The $formName parameter is relative to the current user. In this way, only the "clicked" form is submitted. Wish it helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?