2016-09-11 17 views
1

我有一種將條形碼與資產關聯的表單。Symfony 3 - CollectionTypeType實體類型 - Ajax提交(FOS REST)

可以有多個條形碼與每個資產相關聯。

實體條目資產和條形碼是:

資產實體 - >條碼

/** 
* @var ArrayCollection $barcodes 
* @ORM\ManyToMany(targetEntity="Barcode", cascade={"persist"}) 
* @ORM\OrderBy({"updated" = "DESC"}) 
* @ORM\JoinTable(name="asset_barcode", 
*  joinColumns={@ORM\JoinColumn(name="asset_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="barcode_id", referencedColumnName="id", unique=true, nullable=false)} 
*  ) 
*/ 
protected $barcodes; 

條碼實體 - >條碼

/** 
* @var string 
* 
* @ORM\Column(type="string", length=64, nullable=true) 
* @ORM\ManyToMany(targetEntity="Asset", mappedBy="barcodes", cascade={"persist", "remove"}) 
*/ 
private $barcode; 

AssetType形式

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
      ->add('id', HiddenType::class, ['label' => false]) 
      ->add('serial_number', TextType::class, ['label' => false]) 
      ->add('model', TextType::class, [ 
       'label' => 'common.model' 
      ]) 
      ->add('location', EntityType::class, [ 
       'class' => 'AppBundle:Location', 
       'choice_label' => 'name', 
       'multiple' => false, 
       'expanded' => false, 
       'required' => true, 
       'label' => 'asset.location', 
       'choice_translation_domain' => false 
      ]) 
      ->add('barcodes', CollectionType::class, [ 
       'label' => 'asset.barcode', 
       'entry_type' => BarcodeType::class, 
       'by_reference' => false, 
       'required' => false, 
       'label' => false, 
       'empty_data' => null, 
       'allow_add' => true, 
       'allow_delete' => true, 
       'delete_empty' => true, 
       'mapped' => false, 
       'prototype_name' => '__barcode__' 
      ]) 
      ->add('comment', TextType::class, [ 
       'label' => false 
      ]) 
      ->add('active', CheckboxType::class, ['label' => 'common.active']) 
    ; 
    $builder->get('model') 
      ->addModelTransformer(new ModelToIdTransformer($this->em)); 
    $builder->get('barcodes') 
     ->addModelTransformer(new BarcodeToEntityTransformer($this->em)); 
} 

BarcodeToEntityTransformer正在接收null數據。我通過使用轉儲來確定。

BarcodeType形式

class BarcodeType extends AbstractType 
{ 

    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
       ->add('id', HiddenType::class, ['label' => false]) 
       ->add('updated', HiddenType::class, ['label' => false, 'disabled' => true]) 
       ->add('barcode', TextType::class, [ 
       ]) 
       ->add('comment', TextType::class, [ 
        'label' => false 
       ]) 
     ; 
    } 

    /** 
    * @param OptionsResolver $resolver 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Barcode' 
     )); 
    } 

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

} 

條形碼原型

<div id="asset_barcodes" data-prototype=" <div class="form-row barcode"><input type="hidden" id="asset_barcodes___barcode___updated" name="asset[barcodes][__barcode__][updated]" disabled="disabled" /><span class="input"><input type="text" id="asset_barcodes___barcode___barcode" name="asset[barcodes][__barcode__][barcode]" /></span><span class="comment"><input type="text" id="asset_barcodes___barcode___comment" name="asset[barcodes][__barcode__][comment]" /></span><span class="remove-form-row" title="Remove" id="barcode-__barcode__"><i class="fa fa-remove"></i></span></div> "></div> 

我的問題是,條形碼數據似乎並不時應用/ JSON提交待觀察。

JSON data

有一點點額外的數據被髮送的頁面架構的副作用。

應該如何提交條形碼數據以便通過表單正確讀取?或者,我需要對錶單進行哪些更改?

+0

爲什麼你在'AssetType'表單中有''mapped'=> false'? – Nevertheless

回答

1

的問題是,很顯然,與'mapped' => false,

該選項用於表單的字段,這是無關的模型(FE一個「接受規則」複選框或這樣的東西)

Symfony在這種情況下不會以任何方式處理來自此字段的數據,也不會將其設置爲模型。