我的多选字段出了问题。这是代码:
在实体类
中/**
* @Annotation\Options({ "disable_inarray_validator":"true", "label":"Bound Checkpoints", "target_class":"Checkpoint","property":"name"})
* @Annotation\Type("DoctrineORMModule\Form\Element\EntitySelect")
* @Annotation\Attributes({ "multiple":"true", "class":"form-control"})
* @Annotation\Required(false)
* @ORM\ManyToMany(targetEntity="Checkpoint", inversedBy="affectedByCheckpoints")
*/
private $boundCheckpoints;
public function __construct() {
$this->boundCheckpoints= new \Doctrine\Common\Collections\ArrayCollection();
}
public function addBoundCheckpoints( $items)
{
foreach($items as $item)
{
$this->boundCheckpoints->add($item);
}
}
public function removeBoundCheckpoints($items)
{
foreach($items as $item)
{
$this->boundCheckpoints->removeElement($item);
}
}
我的问题是:如果我将一些东西放入现场,它会保存得很好。如果我删除一个,仍在工作。但是,如果我删除我设置的所有项目并触发表单,则永远不会调用removeBoundCheckpoints方法。
我尝试在表单中设置一个必需的验证器,但如果我这样做,我就会遇到验证问题。
有什么想法吗?
答案 0 :(得分:0)
通过将此变通方法添加到模板中来解决。
{% if field.getAttribute('multiple') == 'multiple' or field.getAttribute('multiple') == 'true' %}
<input type="hidden" name="{{field.getName()}}" value="" />
{% endif %}
{{formElement(field) }}