2017-06-18 56 views
0

我使用select2插件与Ajax在我的窗体上有一个动态字段,但是当我提交它时,返回错误“此值为无效“,这是正常的原因,我使用ChoiceType与创建choices选项中的空array()。根据symfony文档的this部分,表单事件是我的救世主,所以试图使用它,但它看起来像我的代码有问题,并不能真正看到什么。Symfony,如何使用窗体事件来验证动态客户端窗体

我的问题是:

怎样的选择可能传递到字段中,形式是有效的。

我的表单类型

class ArticleType extends AbstractType 
{ 
    /** 
    * {@inheritdoc} 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      //My other field 

     //My functions to add the field with the possible choices 
     $formModifier = function (FormInterface $form, $imageValue) use ($options) { 
      if ($imageValue !== null) { 
       $listImages = $this->getChoiceValue($imageValue, $options); 

       if (!$listImages) { 
        $form->get('image')->addError(new FormError(
        'Nous n\'avons pas pu trouver l\'image, veuiller choisir une autre' 
        )); 
       } 
      } else { 
       $listImages = array(); 
      } 

      //die(var_dump($listImages)); //Array of Image 

      $form->add('image', ChoiceType::class, array(
       'attr' => array(
        'id' => 'image'), 
       'expanded' => false, 
       'multiple' => false, 
       'choices' => $listImages)); 
     }; 

     $formModifierSubmit = function (FormInterface $form, $imageValue) use ($options) { 
      if ($imageValue !== null) { 
       $listImages = $this->getChoiceValue($imageValue, $options); 

       if (!$listImages) { 
        $form->get('image')->addError(new FormError(
         'Nous n\'avons pas pu trouver l\'image, veuiller choisir une autre' 
        )); 
       } 
      } else { 
       $form->get('image')->addError(new FormError(
        'Veuillez choisir une image s.v.p.' 
       )); 
      } 

      //die(var_dump($listImages)); //Array of Image object 

      $config = $form->get('image')->getConfig(); 
      $opts = $config->getOptions(); 
      $chcs = array('choices' => $listImages); 
      //die(var_dump($chcs)); //output an array with a 'choices' keys with array value 
      array_replace($opts, $chcs); //not work 
      //array_merge($opts, $chcs); //not work 
      //die(var_dump($opts)); //replacements/merge are not made 
     }; 

     $builder->addEventListener(
      FormEvents::PRE_SET_DATA, 
      function (FormEvent $event) use ($formModifier) { 
       // this would be the entity Article 
       $data = $event->getData(); 

       $formModifier($event->getForm(), $data->getImage()); 
      } 
     ); 

     //$builder->get('image')->addEventListener(//give error cause the field image don't exist 
     $builder->addEventListener(
      FormEvents::PRE_SUBMIT, 
      function (FormEvent $event) use ($formModifierSubmit) { 
       $imageVal = $event->getData(); 
       //die(var_dump($imageVal)); //return all the submitted data field in an array 
       //But when change this event to Submit it return the Article model populated by the submitted data, EXCEPT the image field which have null as value 

       $formModifierSubmit($event->getForm(), $imageVal['image']); 
      } 
     ); 
    } 

    public function getChoiceValue($imageValue, $options) 
    { 
     $listImages = $options['em']->getRepository('AlmotivAppBundle:Image')->findBy(array(
      'id' => $imageValue 
     )); 

     return $listImages; //array of Image object 
    } 
    [...] 
} 

对于信息

image场不依赖于像文档的任何其它领域,所以我需要填充上PRE_SUBMIT事件choices选项给出可能的选择。

而且还image有我Article实体多对一关系

class Article implements HighlightableModelInterface 
{ 
    //some properties 
    /** 
    * @ORM\ManyToOne(targetEntity="Image\Entity\Path", cascade={"persist"}) 
    * @Assert\Valid() 
    */ 
    private $image; 
} 

如果我在糟糕的方式就是让我知道,因为我现在出来的想法,我尝试很多东西,像

  • array_replace在字段配置中的选项,但没有错。
  • 向表单动作url : $form.attr('action')的网址发出ajax请求,我认为它会加载choices选项,并且可能有<option>,但我的选择仍然返回没有<option>

以及更多(不能remmenber)。

而且我在select2插件的v4.0.3中使用了框架的v3.1,如果需要更多的信息,只需要问和thx阅读和尝试帮助。

编辑

只需添加一些信息

回答

0

你做的事情太复杂更清晰。在您的文档示例中,他们为已有的表单字段('sport')添加eventListener,并将其添加到稍后添加的不存在的字段(文档示例中的'image'字段和'position'字段)。

您应该使用EntityType,如果你需要(这我不知道如果你是)使用query_builder选项筛选图像,添加验证constraintsexample with controller)。

class ArticleType extends AbstractType { 
/** 
* {@inheritdoc} 
*/ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 

     // $builder 
     // My other field 
     $imageFieldFunction = $this->getImageFieldFunction(); 

     $builder->addEventListener(FormEvents::PRE_SET_DATA, $imageFieldFunction); 
     $builder->addEventListener(FormEvents::PRE_SUBMIT, $imageFieldFunction); 

    } 
    private function getImageFieldFunction() 
    { 
     return function(FormEvent $event) { 
      $form = $event->getForm(); 
      $data = $event->getData(); 
      //when your data_class is Article 
      $image = $data->getImage();//depending on your Article class 
      /*if you are using data_class => null 
      $image = $data['image']; 
       */ 
      $imageId = $image ? $image->getId() : 0; 

      $builder->add('image', EntityType::class , array(
      'class' => 'AlmotivAppBundle:Image', 
      'attr' => array(
       'id' => 'image' 
      ) , 
      'expanded' => false, 
      'multiple' => false, 
      'constraints' => new NotBlank(), 
      'query_builder' => function (EntityRepository $er) use ($imageId) { 
        return $er->createQueryBuilder('i') 
          ->where('i.id = :image_id') 
          ->setParameter('image_id', $imageId); 
      } 

     )); 
     } 
    } 
} 
+0

我不认为使用'EntityType'是最好的方法。我的表单上有一个与我的Article实体有ManyToOne关系的字段图像,但图像表可能有数千个条目,所以我使用ajax(使用select2插件)在客户端传递选择可能性, m在字段的选项选项中使用空数组,然后我想将选项传递给字段以使表单有效 –

+0

那么您可以使用EntityType进行ajax加载。我自己实际上遇到了这个问题,而且我的做法是通过向构建器添加eventListener来处理PRE_SET_DATA和PRE_SUBMIT事件。我已经更新了我的答案。 –

+0

它适用于我,我确定我正在做这样的事情,但无论如何,它完美的工作。所以事情就是用EntityType来工作,而我正在路上,我的意思是我想。我用我的最终代码发布答案。 Thx寻求帮助 –