doushui20090526 2016-09-11 03:05
浏览 40
已采纳

Symfony 3 - EntityType的CollectionType - Ajax提交(FOS REST)

I have form which is associating barcodes with assets.

There can be multiple barcodes associated with each asset.

The Entity entries for the Asset and Barcodes are:

Asset Entity -> barcodes

/**
 * @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;

Barcode Entity -> barcode

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

AssetType form

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 ) );
}

The BarcodeToEntityTransformer is receiving null data. I determined this by using dump and die.

BarcodeType form

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';
    }

}

barcodes prototype

<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>

My problem is that the barcode data does not seem to be seen when submitted with application/json.

JSON data

There is a little extra data being sent as a side effect of the page architecture.

How should the barcodes data be submitted in order to be read properly by the form? Or, what changes do I need to make to the form?

  • 写回答

1条回答 默认 最新

  • du656637962 2016-09-11 09:17
    关注

    The problem is, apparently, with 'mapped' => false,

    This option is used for the fields of the form, which are unrelated to a model (f.e. for a "accept rules" checkboxes or stuff like this)

    Symfony in this case does not process data from this field in any way and does not set it to model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低
  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复