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.

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

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历