I'm building an API with the FOSRestBundle and I'm trying to add existing Employee
entities to an existing Report
entity. But my form validation keeps throwing an error: This form should not contain extra fields
Adding 'allow_add' => true
to the ReportType options is not an option because I only want to add excisting employees to the Report. How can I make this work?
PUT request body
{
"report":{
"notes":"This is a note.",
"client":5,
"contactPerson":4,
"contributors":[
{
"id": 10
}
]
}
}
ReportType
class ReportType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('notes')
->add('client', 'entity', array(
'class' => 'ApiBundle:Client'
))
->add('contactPerson', 'entity', array(
'class' => 'ApiBundle:ContactPerson'
))
->add('contributors', CollectionType::class, array(
'entry_type' => EmployeeType::class,
'by_reference' => false
))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ApiBundle\Entity\Report',
'csrf_protection' => false,
'allow_extra_fields' => true
));
}
}
Snippet from Report entity
/**
* @ORM\ManyToMany(targetEntity="ApiBundle\Entity\Employee", inversedBy="contributions", cascade={"persist"})
* @ORM\JoinTable(name="reports_contributors")
*/
private $contributors;
/**
* Constructor
*/
public function __construct()
{
$this->contributors = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add contributor
*
* @param \ApiBundle\Entity\Employee $contributor
*
* @return Report
*/
public function addContributor(\ApiBundle\Entity\Employee $contributor)
{
$this->contributors[] = $contributor;
return $this;
}
/**
* Remove contributor
*
* @param \ApiBundle\Entity\Employee $contributor
*/
public function removeContributor(\ApiBundle\Entity\Employee $contributor)
{
$this->contributors->removeElement($contributor);
}
/**
* Get contributors
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getContributors()
{
return $this->contributors;
}
Snippet from Employee Entity
/**
* @ORM\ManyToMany(targetEntity="ApiBundle\Entity\Report", mappedBy="contributors")
*/
private $contributions;
public function __construct() {
$this->contributions = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add contribution
*
* @param \ApiBundle\Entity\Report $contribution
*
* @return Employee
*/
public function addContribution(\ApiBundle\Entity\Report $contribution)
{
$this->contributions[] = $contribution;
return $this;
}
/**
* Remove contribution
*
* @param \ApiBundle\Entity\Report $contribution
*/
public function removeContribution(\ApiBundle\Entity\Report $contribution)
{
$this->contributions->removeElement($contribution);
}
/**
* Get contributions
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getContributions()
{
return $this->contributions;
}
Solved it!
No need to use CollectionType
. The solution is to use an EntityType
with the multiple
option.
->add('contributors', 'entity', array(
'class' => 'ApiBundle:Employee',
'multiple' => true
))
PUT request body
{
"report":{
"notes":"This is a note.",
"client":5,
"contactPerson":4,
"contributors":[10,6]
}
}