FormCollection doesn't allow removing the last element #4094

Closed
Jasu opened this Issue Mar 22, 2013 · 1 comment

Comments

Projects
None yet
2 participants
@Jasu

Jasu commented Mar 22, 2013

FormCollection doesn't allow removing the last element, even if allow_remove is set.

This is caused by the check

    // Can't do anything with empty data
    if (empty($data)) {
        return;
    }

being before the handling of removed elements.

@simogrima

This comment has been minimized.

Show comment Hide comment
@simogrima

simogrima Feb 9, 2014

I solved by setting an empty collection
something like this (Doctrine example)

//..        
if ($this->request->isPost()) {
    $postedData = $this->request->getPost();
    $form->setData($postedData);
    if ($form->isValid()) {
         if (!isset($postedData['role']['permissions'])) {
            $col = new \Doctrine\Common\Collections\ArrayCollection();
            $role->setPermissions($col);
        }    
        $this->roleMapper->update($role);
    }
}
//..

I solved by setting an empty collection
something like this (Doctrine example)

//..        
if ($this->request->isPost()) {
    $postedData = $this->request->getPost();
    $form->setData($postedData);
    if ($form->isValid()) {
         if (!isset($postedData['role']['permissions'])) {
            $col = new \Doctrine\Common\Collections\ArrayCollection();
            $role->setPermissions($col);
        }    
        $this->roleMapper->update($role);
    }
}
//..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment