2016-06-01 11 views
2

私はSymfony2フレームワーク(特にSymfony 2.7)を使用しています。私の質問はフォーム構築に関連しています。Symfony2フォーム:フォームの値に応じてデフォルトのオプションを設定します

私はという場所というエンティティを持っています。これは私のプロジェクトの他の多くのエンティティに関連付けることができます。そこで、私はアプリケーションの多くの部分で再利用できるカスタムフォームタイプを作成しました。

class PlacesType extends AbstractType 
{ 

    private $security_context; 

    public function __construct(SecurityContext $security_context) 
    { 
     $this->security_context = $security_context; 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $user = $this->security_context->getToken()->getUser(); 

     parent::configureOptions($resolver); 

     $resolver->setDefaults(array(
      'class' => 'AppBundle\Entity\Places', 
      'query_builder' => function (EntityRepository $repository) use ($user) 
      { 
       return $repository->queryPlacesForUser($user); 
      }, 
     )); 

    } 

    public function getParent() 
    { 
     return 'entity' ; 
    } 

    public function getName() 
    { 
     return 'places'; 
    } 
} 

事実が場所はソフトに除去することが可能である(私はクラス内部フラグを削除設定しました)。したがって、新しいエンティティはアクティブな場所にのみ関連付けることができますが、古いエンティティは現在削除された場所との関連付けを維持できます。

このため、私は場所がすでに親のフォームデータに関連付けられた場合を除きqueryPlacesForUser機能は、唯一アクティブ場所を返すことにしたいです。このような
何か:残念ながら

public function configureOptions(OptionsResolver $resolver) 
    { 
     // ... 

     $currdata = $this->getForm()->getData(); // pseudo-code, this does not work 

     $resolver->setDefaults(array(
      'class' => 'AppBundle\Entity\Places', 
      'query_builder' => function (EntityRepository $repository) use ($user) 
      { 
       return $repository->queryPlacesForUser($user, $currdata); 
      }, 
     )); 

    } 

、私はオプションリゾルバから現在のデータを取得する方法を見当もつかない。フォームのbuildForm関数内の現在のデータを取得することは可能ですが、内部でフォームオプションを設定することはできません。

フォームに渡されたデータを使用してフォームのデフォルトオプションを設定できる方法はありますか?

+0

あなたは*あなたがしているあなたのコード内のその時点で*必要な構成データを注入することをいとわないだろうこのカスタムフォームタイプを使用していますか? – Yoshi

+0

いいえ、私はそれについて考えましたが、あなたは共有フォームタイプを持つ利点を失います。 – Alberto

+0

正直なところ、それがあなたの唯一の選択だと思う。そして、それはフォームデータを注入するのとはまったく異なりますか?私が意味することを示す答えを追加します。 – Yoshi

答えて

0

そうのようなイベントリスナーを使用することができます:あなたはconfigureOptions内のフォームデータにアクセスすることはできません

$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { 
        $data = $event->getData(); 
        $form = $event->getForm(); 
    //check your data here and add the fields you want accordingly 

$field = $builder->get('fieldToModify');   // get the field 
$options = $field->getOptions();   // get the options 
$type = $field->getType()->getName();  // get the name of the type 
$options['label'] = "Login Name";   // change the label 
$builder->add('fieldToModify', $type, $options); // replace the field 
     } 
+0

あなたの答えをありがとうが、私はフォームに子供を追加しようとしていないが、現在のオプションを設定する。 – Alberto

+0

うーん...おそらく私は書いて、それに応じてフィールドを変更する必要がありました。 –

2

事前設定/再設定拡張タイプのみを追加したいという制約が追加されたため、このカスタムフォームタイプが使用されている場所に必要な設定を抽出することが唯一の選択肢です。例:

使用PlacesType

<?php 

/** 
* @inheritdoc 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $this->builder->add('place', PlacesType::class, [ 
     'only_active' => false // <- this is the *external* configuration 
    ]); 
} 

<?php 

/** 
* @inheritdoc 
*/ 
public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'class' => 'AppBundle\Entity\Places', 
     'only_active' => true, // <- this is the *default* configuration 

     // note the extra closure, this gives you access to the *resolved* options. 
     'query_builder' => function (Options $options) { 
      return function (EntityRepository $repository) use ($options) { 
       return $repository->queryPlacesForUser(
        $this->security_context->getToken()->getUser(), 
        $options['only_active'] 
       ); 
      }; 
     }, 
    )); 
} 
+0

ありがとうございました。私はあなたの答えを使用しました(私はオプションの中で現在の場所を直接渡しました)が、私はまだそれをあまり好きではありません。私は_places_フォームタイプを使用していた場所を20種類以上変更しなければならず、プロジェクトの他のエンティティも同じように動作します。私はよりスケーラブルなソリューションを好むだろう。 – Alberto

+0

@Alberto私はその特定の*道路ブロック*を何度も理解しています。この問題は実際には、基礎となるデータに基づいて拡張タイプのデフォルトを変更したいという事実からのみ発生します。今日まで、私は本当により良い解決策を見いだすことはできませんでしたが、私は本当にそれを見たいと思っています。 – Yoshi

関連する問題