symfony2:如何使用formType表选择一个字段进行查看

时间:2015-04-29 11:29:42

标签: ajax symfony

我想使用ajax显示一个字段来编辑此字段,但我在formtype中有表格的所有字段 这是我的表格类型

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $options = array('multiple' => 'multiple');
        $builder
                ->add('name', 'text', array(
                    'label' => 'Company Name',
                ))
                ->add('abbreviation', 'text', array(
                    'required' => false,
                    'label' => 'Abbreviation',
                ))
                ->add('description', 'textarea', array(
                    'required' => false,
                ))
                ->add('city', 'text', array(
                    'required' => false,
                ))
                ->add('address', 'text', array(
                    'required' => false,
                ))
                ->add('facebook', 'url', array(
                    'required' => false,
                ))
                ->add('linkedin', 'url', array(
                    'required' => false,
                ))
                ->add('twitter', 'text', array(
                    'required' => false,
                ))
                ->add('youtube', 'url', array(
                    'required' => false,
                ))
                ->add('googleplus', 'text', array(
                    'required' => false,
                ))
                ->add('skype', 'text', array(
                    'required' => false,
                ))
                ->add('chairman', 'text', array(
                    'required' => false,
                ))
                ->add('managingdirector', 'text', array(
                    'required' => false,
                ))
                ->add('contactperson', 'text', array(
                    'required' => false,
                ))
                ->add('contactpersontitle', 'text', array(
                    'required' => false,
                ))
                ->add('contactpersonphone', 'text', array(
                    'required' => false,
                ))
                ->add('isActivated', 'checkbox', array(
                    'required' => false,
                ))
                ->add('countryId', 'entity', array(
                    'label' => 'Country',
                    'class' => 'Custom\CMSBundle\Entity\Country',
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('u')
                                ->orderBy('u.name', 'ASC');
                    }
                ))
                ->add('owner')
                ->add('class')
                ->add('level', 'choice', array(
                    'choices' => array(
                        '1' => '1',
                        '2' => '2',
                        '3' => '3',
                        '4' => '4',
                        '5' => '5',
                        '6' => '6',
                        '7' => '7',
                        '8' => '8',
                        '9' => '9',
                        '10' => '10',
                    )
                ))
                ->add('phonechairman', 'text', array(
                    'required' => false,
                ))
                ->add('mobilechairman', 'text', array(
                    'required' => false,
                ))
                ->add('faxchairman', 'text', array(
                    'required' => false,
                ))
                ->add('emailchairman', 'text', array(
                    'required' => false,
                ))
                ->add('phonemanagingdirector', 'text', array(
                    'required' => false,
                ))
                ->add('mobilemanagingdirector', 'text', array(
                    'required' => false,
                ))
                ->add('faxmanagingdirector', 'text', array(
                    'required' => false,
                ))
                ->add('emailmanagingdirector', 'text', array(
                    'required' => false,
                ));
    }

如何使用ajax返回一个字段的表单编辑 如果答案隐藏了该字段,我该如何设置不隐藏的字段

1 个答案:

答案 0 :(得分:0)

我不知道你想用Ajax编辑哪个字段,但假设它是“name”。 并假设您的实体是Entity1(Entity1.php,如User.php是一个实体)。

然后,您必须为此用例创建特定的editForm。 示例:editEntity1Form1(选择最佳名称)

然后editEntity1Form1将具有:

public function buildForm(FormBuilderInterface $builder, array $options) {
        $options = array('multiple' => 'multiple');
        $builder
            ->add('name', 'text', array(
                'label' => 'Company Name',
            ));
}

/**
 * @param OptionsResolverInterface $resolver
 * It specifies the entity corresponding to your editing form
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'DIRECTORY_TO_YOUR_ENTITY\Entity1'
    ));
}

现在在你的控制器中,你必须在使用EditEntity1Form1的ajax编辑动作中进行speficy:

/**
* Creates a form to edit an Entity1 entity.
*
* @param Entity1 $entity The entity
* your_form_route_name_here is the route name of the action you want to associate to your edit form (if you submit your edit form, it will make a post with this route. But you can get this action route in you twig view and Post it in an ajax way as you want.
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Entity1 $entity)
{
    $form = $this->createForm(new EditEntity1Form1(), $entity, array(
        'action' => $this->generateUrl('your_form_route_name_here', array('id' => $entity->getId())),
        'method' => 'POST',
    ));

    return $form;
}

对于您的控制器,您可以查看:http://intelligentbee.com/blog/2015/01/19/symfony-2-forms-and-ajax/

在您的ajax POST操作中,您将拥有以下这些行:

 $editForm = $this->createEditForm($entity);
 $form->handleRequest($request);

这些行指定使用editEntity1Form1。 $ entity来自:

$entity = $em->getRepository('YOURBundle:Entity1')->find($id);

将$ id传递给您的帖子ajax操作。