2016-03-26 7 views
0

どうすればいいですか事前選択entityフィールドタイプを使用していますか?Symfony2 - エンティティのフィールドタイプのデフォルトの選択肢を設定するには?

ほとんどのユーザーが同じ国に住んでいるため、データベースから取得した国の1つを事前に選択したいと考えています。

dataオプションを過去に追加して、checkboxフィールドの値を事前に選択することができました。

このLocationTypeフォームは、フォームが複数のエンティティから構築されているため、UserTypeフォームに含まれています。

LocationType形式:

namespace Cmp\MyBundle\Form\Type; 

use Cmp\MyBundle\Entity\Account; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\Validator\Constraints as Assert; 
use Doctrine\ORM\EntityRepository; 

    class LocationType extends AbstractType 
    { 
     public function buildForm(FormBuilderInterface $builder, array $options) 
     { 

      $builder->add('company') 
       ->add('street') 
       ->add('streetno') 
       ->add('streetsuffix') 
       ->add('zipcode') 
       ->add('city') 
       ->add('province') 
       ->add('country', 'entity', array(
        'class' => 'CmpMyBundle:Country', 
        'query_builder' => function (EntityRepository $er) { 
         return $er->createQueryBuilder('c') 
          ->orderBy('c.nameEn', 'ASC'); 
        }, 
        'choice_label' => 'nameEn', 
       )) 
       ->add('tel') 
       ->add('fax') 
       ->add('url') 
       ->add('account', new AccountType()); 
     } 

     public function configureOptions(OptionsResolver $resolver) 
     { 
      $resolver->setDefaults(array(
       'data_class' => 'Cmp\MyBundle\Entity\Location', 
      )); 
     } 

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

草のcoutryリストdorpdownに生成小枝。フォームを生成

 <div class="form-group {% if not form.location.country.vars.valid %}has-error{% endif %}"> 
      {{ form_label(form.location.country, 'country'|trans, {'label_attr': {'class': 'col-sm-3 control-label'}}) }} 
      <div class="col-sm-9"> 
       {{ form_errors(form.location.country) }} 
       {{ form_widget(form.location.country, {'attr': {'class': 'form-control', 'placeholder': 'country'|trans}}) }} 
      </div> 
     </div> 

コントローラー:

​​

ユーザ型形態は、上に示した場所フォームタイプを含みます。

答えて

0

あなたは、たとえば、そのフィールド

に値を割り当てることができ、あなたのフォームを作成します。

$data = new Foo(); 
$data->setCountryCode($country); 
$form = $this->createForm(new myFormType(), $data); 
+0

私の場合私のフォームは2つのエンティティの組み合わせであるため、これは機能しません。これらのエンティティはManyToOneリンクにリンクされています。したがって、新しいフォームはユーザーから始まり、そのアカウントを含みます。したがって、私が設定したいデフォルト値は、新しいフォームが始まるものと同じ他のエンティティ(アカウント)にあります。 – Tom

0

はこれを試してみてください:

namespace Cmp\MyBundle\Form\Type; 

use Cmp\MyBundle\Entity\Account; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\Validator\Constraints as Assert; 
use Doctrine\ORM\EntityRepository; 

class LocationType extends AbstractType 
{ 
    protected $er; 

    public function __construct(EntityRepository $er) 
    { 
     $this->er = $er; 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 

     $builder->add('company') 
      ->add('street') 
      ->add('streetno') 
      ->add('streetsuffix') 
      ->add('zipcode') 
      ->add('city') 
      ->add('province') 
      ->add('country', 'entity', array(
       'class' => 'CmpMyBundle:Country', 
       'query_builder' => function (EntityRepository $er) { 
        return $er->createQueryBuilder('c') 
         ->orderBy('c.nameEn', 'ASC'); 
       }, 
       'data' => $this->er->FindOneBy(['country_code' => 'nl']) 
       'choice_label' => 'nameEn', 
      )) 
      ->add('tel') 
      ->add('fax') 
      ->add('url') 
      ->add('account', new AccountType()); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'Cmp\MyBundle\Entity\Location', 
     )); 
    } 

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

あなたは国のレポを注入する必要がありますサービス定義:

services: 
    app.country_repository: 
     class: Doctrine\ORM\EntityRepository 
     factory_service: doctrine.orm.default_entity_manager 
     factory_method: getRepository 
     arguments: 
      - YOUR\COUNTRY\NAMESPACE\Country 
    app.form.type.location: 
     class: Cmp\MyBundle\Form\Type\LocationType 
     arguments: 
      - "@app.country_repository" 
     tags: 
      - { name: form.type } 
+0

それは論理的に聞こえますが、どうすればいいのですか?私は 'データ'フィールドの背後にある次のコードを試してみましたが、PHPエラーが発生しました:$ this-> getDoctrine() - > getRepository( 'MyBundle:Country') - > findOneBy(array( 'countryCode' => "NL")) – Tom

+0

フォームはサービスとして定義されていますか?それとも、コントローラーでビルドしますか?フォームを作成する場所の完全なクラスを投稿できますか? – gvf

+0

これはformtypeとして定義されています。このfromtypeの完全なコードを追加しましたか? – Tom

関連する問題