2016-09-19 18 views
0

バージョン2.8.11のSymfonyがあります。私は2つのボタンを使ってフォームを作成しています。「ボタン1をクリックすると、あるルートにリダイレクトし、ボタン2をクリックして別のルートにリダイレクトする」のような処理をしたいだけです。 documentationでは、isClicked()というメソッドが見つかりました。問題は、それが存在しないようなメソッドを呼び出すことができないことです(as seen in attached image)。何が問題なの?そして、あなたのオートコンプリートを持っている。このインタフェースのメソッド - 一般的なリターンのSymfony 2.8フォームボタンがクリックされました

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

答えて

2

機能$form->getForm()は、インタフェースFormInterfaceを実装するオブジェクト。

しかし、このオブジェクトの一部は、追加のメソッドを持つことができ、あなたの$form->get('delete')このメソッドが存在しますが、$form->get('admins')ことはないでしょうのため\Symfony\Component\Form\SubmitButtonだからFormInterfaceともClickableInterface方法isClicked

を実装しています。

あなたはオートコンプリートを持っているしたい場合は、あなたのIDEでサポートされるべきたPHPDoc注釈、と方法をキャストすることができます

/** @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)

+0

問題は、 'isClicked()'メソッドがまったく存在しないことです。これは、IDEによって自動補完されていないと私はちょうどで入力した場合、私は、エラー、次のしている: 'お知らせ:未定義のプロパティ:Symfonyの\コンポーネント\フォーム\サブミット:: $ isClicked 500内部サーバーエラー - ContextErrorException' – Kamil

+0

が実際にそれを働いています $ form = $ this-> createFormBuilder($ admin) - > add( 'save'、SubmitType :: class、array( 'label' => 'Create Task')) (* 'SaveAndAdd'、SubmitType :: class、array( 'label' => '保存して追加')) - > getForm(); ' ** AdminController.php **の代わりに* * AdminIdType.php **。私が物事をどのように入力しても、オートコンプリートはまだありません。それは変です。 – Kamil

+0

ああ、それは簡単なことです。 UPD –

関連する問題