2016-11-30 52 views
3

我有一個圖像字段是可選的。當你上傳圖片時,它會將文件名保存在數據庫中(通過教條使用事件)。如何使Symfony2窗體的可選字段保存爲空

問題是,當您編輯已經上傳的表單並且不添加圖像時,它會使圖像字段爲空。

有沒有辦法檢查/刪除字段值設置爲null,如果沒有圖像上傳?

實體,表單代碼如下:

class Product 
{ 

    /** 
    * @ORM\Column(type="string", nullable=true) 
    * 
    * @Assert\Image 
    */ 
    private $image; 
} 

形式

class ProductType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('image', FileType::class, [ 
      'required' => !$options['update'], 
     ]); 

     $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { 
      $form = $event->getForm(); 
      $product = $event->getData(); 

      if (null == $form->get('image')->getData()) { 
       // $form->remove('image'); 
      } 
     }); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Product' 
     )); 
    } 

    public function getBlockPrefix() 
    { 
     return 'appbundle_product'; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setRequired([ 
      'update', 
     ]); 
    } 
} 

//在控制器

$editForm = $this->createForm(
    'AppBundle\Form\ProductType', 
    $product, 
    [ 
     'update' => true 
    ] 
); 

回答

2

您需要的情況下PRE_SUBMIT,試試這個:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('image', FileType::class, [ 
     'required' => !$options['update'], 
    ]); 

    $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { 
     $form = $event->getForm(); 
     $product = $event->getData(); 

     if(empty($product['image'])){ 
      //No new image for existing field was given. Make field disabled so doctrine will not update with empty 
      $config = $event->getForm()->get('image')->getConfig(); 
      $options = $config->getOptions(); 
      $event->getForm()->add(
       'image', 
       $config->getType()->getName(), 
       array_replace(
        $options, 
        ['disabled' => true] 
       ) 
      ); 
     } 
    }); 
} 
+0

非常感謝你。 –

+0

爲了記錄:取代'組$ config-> getType->的getName()'(2.X)使用'get_class(組$ config->的getType() - > getInnerType())'在3.X –

0

Symfony的形式有提交()方法,它通過默認設置爲true有兩個參數,$ submittedData和$ clearMissing

/** 
* Submits data to the form, transforms and validates it. 
* 
* @param null|string|array $submittedData The submitted data 
* @param bool    $clearMissing Whether to set fields to NULL 
*           when they are missing in the 
*           submitted data. 
* 
* @return FormInterface The form instance 
* 
* @throws Exception\AlreadySubmittedException If the form has already been submitted. 
*/ 
public function submit($submittedData, $clearMissing = true); 

$ clearMissing參數。

如果您在您的控制器中執行此操作:$form->submit($submittedData, false);,您的圖像將不會設置爲空。

https://symfony.com/doc/current/form/direct_submit.html#calling-form-submit-manually

類似的問題:Symfony2 - How to stop Form->handleRequest from nulling fields that don't exist in post data

+0

謝謝你指向另一個問題/答案。不知何故,我錯過了我的搜索條件。我還喜歡關於那裏的文件處理的一些意見。不知何故數據在文件上傳時缺失。我可能想嘗試更多的錯誤。只是指出。 –

相關問題