Kamil
Kamil

Reputation: 111

Symfony 2.8 Form Button isClicked

I have Symfony in version 2.8.11. I'm creating form with two buttons and I just want to do something like "If button 1 is clicked, redirect to some route, if button 2 is cicked, redirect to another route". In documentation I found method called isClicked(). The problem is that I can't call such method as it's don't exist (as seen in attached image). What could be the problem?

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())
        );
    }
}

Upvotes: 0

Views: 1614

Answers (1)

Dmitry Malyshenko
Dmitry Malyshenko

Reputation: 3051

Function $form->getForm() in general returns object which implements interface FormInterface - and methods of this interface you have in your autocomplete.

But some of this objects can have additional methods, and \Symfony\Component\Form\SubmitButton implements FormInterface and also ClickableInterface with a method isClicked

So for your $form->get('delete') this method will exist, but for $form->get('admins') it will not.

If you want to have an autocomplete, you can cast a method with PHPDoc annotation, which should be supported by your IDE:

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

UPD: And pay your attention: if ($form->get('delete')->isClicked()), not if ($form->get('delete')->isClicked)

Upvotes: 3

Related Questions