douwei1408
2016-03-02 06:58
浏览 53
已采纳

在Symfony2的表单类型中添加默认值和实体

I am a totally new in symfony2. I met a situation which I can't solve till now. I have a controller, formtype and eventsubscriber in my project. The form builds by binding the entities. In that case for a particular entity I need to add a default value along with an ID in one of the form fields. My form type is

$builder->add('breed', EntityType::class, array(
      'label' => 'Breed',
      'class' => 'AppBundle:Masters\Breed',
      'placeholder' => '----Select Breed----',
      'query_builder' => function (EntityRepository $er) {
          return $er->createQueryBuilder('b')
          ->orderBy('b.sortOrder', 'DESC');
      },
     'choice_label' => 'breed',
     ));
$builder->addEventSubscriber(new BreedSubscriber($factory));

My Event Subscriber is

private function addBreed($form, $breedmasterId) {
 $form->add($this->factory->createNamed('breed',EntityType::class,null, array(
  'class'         => 'AppBundle:Masters\Breed',
  'placeholder'   => '----Select Breed--------',
  'choice_label'  => 'breed',
  'required'      =>  TRUE,
  'mapped'        =>  false,
  'query_builder' => function (EntityRepository $repository) use ($breedmasterId) {
         $qb = $repository->createQueryBuilder('bm')
                ->where('bm.breed = :breedmasterId')
                 ->setParameter('breedmasterId', $breedmasterId);
                return $qb;
                },
                'auto_initialize' => false
    )));
}

I need to add a default value such as "General" along with an "id" in the addBreed subscriber and it need to be validate on formsubmission. Thanks in advance for the help.

图片转代码服务由CSDN问答提供 功能建议

我是symfony2中的新手。 我遇到了一个到目前为止我无法解决的情况。 我的项目中有一个控制器,formtype和eventsubscriber。 表单通过绑定实体来构建。 在这种情况下,对于特定实体,我需要在其中一个表单字段中添加默认值和ID。 我的表单类型是

  $ builder-> add('breed',EntityType :: class,array(
'label'=>'Breed',
  'class'=>'AppBundle:Masters \ Breed',
'占位符'=>'----选择Breed ----',
'query_builder'=> function(EntityRepository $ er){  
返回$ er-> createQueryBuilder('b')
  - > orderBy('b.sortOrder','DESC'); 
},
'select_label'=>'breed',
  )); 
 $ builder-> addEventSubscriber(new BreedSubscriber($ factory)); 
   
 
 

My Event Subscriber < pre> 私有函数addBreed($ form,$ breedmasterId){ $ form-&gt; add($ this-&gt; factory-&gt; createNamed('breed',EntityType :: class,null,array(\ n'class'=&gt;'AppBundle:Masters \ Breed', '占位符'=&gt;'----选择品种--------', 'select_label'=&gt;'品种 ', 'required'=&gt; TRUE, 'mapped'=&gt; false, 'query_builder'=&gt; function(EntityRepository $ repository)use($ breedmasterId){ $ qb = $ repository-&gt; createQueryBuilder('bm') - &gt; where('bm.breed =:breedmasterId') - &gt; setParameter('breedmasterId',$ breedmasterId); 返回$ qb ; }, 'auto_initialize'=&gt; false ))); }

我需要在addBreed订阅者中添加一个默认值,例如“General”和“id” 它需要在formubmission上进行验证。 在此先感谢您的帮助。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanchao5258 2016-03-02 07:58
    已采纳

    You can add the following 'data' key in the array you configure your form:

    $builder->add('breed',null, array(
         'label' => 'Breed',
         'data' => 'Sih Tzu'
    ))
    
    打赏 评论

相关推荐 更多相似问题