私は、バーコードをアセットに関連付けるフォームを持っています。Symfony 3 - EntityTypeのCollectionType - 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はヌルデータを受信しています。私はこれをダンプとを使用して決定しました。
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とともに提出する場合バーコードデータが見られていないようだということです。
ページアーキテクチャの副作用として送信されて少し余分なデータがあります。
フォームによって正しく読み取られるためには、バーコードデータをどのように提出する必要がありますか?または、フォームにどのような変更を加える必要がありますか? (「ルールを受け入れる」チェックボックスまたは、このようなもののためにFE)
あなたの 'AssetType'フォームに' mapped '=> 'falseがありますか? – Nevertheless