Symfony 2.8表单按钮被点击

时间:2016-09-19 10:22:16

标签: php forms symfony

我在版本2.8.11中有Symfony。我正在创建带有两个按钮的表单,我只想做“如果点击按钮1,重定向到某个路线,如果按钮2被c,重定向到另一个路线”。在documentation中,我找到了名为isClicked()的方法。问题是我不能调用这样的方法,因为它不存在(as seen in attached image)。可能是什么问题?

AdminIdType.php

<?php

// src/AppBundle/Form/AdminIdType.php
namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;

class AdminIdType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('admins', EntityType::class, array('class' => 'AppBundle:Admin', 'choice_label' => 'idAdmina', 'multiple' => true))
            ->add('save', ButtonType::class, array('label' => 'Create Post'))
            ->add('delete', SubmitType::class, array('label' => 'Delete Post'));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Admin',
        ));

    }
}

AdminController.php

<?php

// src/AppBundle/Controller/AdminRegistrationController.php
namespace AppBundle\Controller;

use AppBundle\Form\AdminType;
use AppBundle\Form\AdminIdType;
use AppBundle\Entity\Admin;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\ClickableInterface;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class AdminController extends Controller
{

    /**
     * @Route("baza/admin/update", name="admin_update")
     * @param Request $request
     * @return type
     */
    public function AdminUpdateAction(Request $request)
    {
        $admin = new Admin();
        $form = $this->createForm(AdminIdType::class, $admin);

        $form->handleRequest($request);

        // HERE I WANTED TO 
        // if ($form->get('delete')->isClicked) {
        //      do smth
        //   }

        return $this->render(
            'adminUpdate.html.twig',
            array('form' => $form->createView())
        );
    }
}

1 个答案:

答案 0 :(得分:2)

函数$form->getForm()通常返回实现接口FormInterface的对象 - 以及您在自动完成中使用的此接口的方法。

但是其中一些对象可以使用其他方法,\Symfony\Component\Form\SubmitButton使用方法FormInterface实现ClickableInterfaceisClicked

因此,对于您的$form->get('delete'),此方法将存在,但对于$form->get('admins')则不会。

如果您想拥有自动完成功能,可以使用PHPDoc注释构建一个方法,IDE应该支持该方法:

/** @var $deleteButton \Symfony\Component\Form\SubmitButton */
$deleteButton = $form->get('delete');
$deleteButton->isClicked(); // now you have autocomplete

UPD: 请注意: if ($form->get('delete')->isClicked()),而非if ($form->get('delete')->isClicked)