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