0

I want to add a delete checkbox to a form.

I have a collection of USER, and I want a checkbox aside each of them so I can (Administrator) delete any user.

But when I integrate a delete checkbox in the form

 ->add('delete','checkbox',array('label'=>'delete','required'=>false,));

the following message is triggered in symfony:

"Neither the property "delete" nor one of the methods "getDelete()", "isDelete()", "hasDelete()", "__get()" exist and have public access in class "Site\BlogBundle\Entity\User".

Should I add a property delete and a getDelete(), so that I can use it?

What is the proper way to do it?

Please take into account in you answer that this is a collection. I want the delete option (via a checkbox) to be integrated for each item (of the collection).

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
Alexis_D
  • 1,908
  • 3
  • 16
  • 35
  • Define what you mean when you say "delete user". What would that actually *do*? – Atli Apr 08 '15 at 15:57
  • Do you mean to add a custom action? This topic might help you out: http://stackoverflow.com/questions/10740303/custom-action-in-sonataadminbundle – Matheno Apr 08 '15 at 15:58

2 Answers2

1

By default, every form builder item is mapped to the entity that it belongs to. This is the mapped attribute. Simply set this attribute to false:

->add('delete', 'checkbox', array(
    'label' => 'delete',
    'required' => false,
    'mapped' => false,
));

Then handle the element in your controller:

$form->handleRequest($request);
$deleteChecked = $form->get('delete')->getData();
// do stuff with $deleteChecked

For a collection I'd imagine something like this:

$form->handleRequest($request);
foreach ($form->get('collection_name') as $item) {
    $deleteChecked = $item->get('delete')->getData();
    // do stuff with $deleteChecked
}

would work, however how to actually handle the deletion is out of my depth as it goes against the grain of how Symfony collections handle deletions.

HOWEVER, why are you creating your own delete checkbox when the collection Form Type has a delete mechanism built into it? If you follow these steps, you should be able to delete items with DOM manipulation.

You could have the checkbox cause a DOM element deletion right before form submission such as an onSubmit action or bind, that way the data still shows right before deletion.

sjagr
  • 15,983
  • 5
  • 40
  • 67
  • Thks sjagr, I used the mapped false option as I do not want to visually delete the user from the view page before it is submited. I have one more question. **How do I get the value of Delete now? As the form contains a collection and for each collection I have a delete checkbox... form->get('delete')->getData() won't be working** – Alexis_D Apr 09 '15 at 09:24
  • this is not working, I already tried and it trigger the following error msg: **Error: Call to a member function get() on array ** – Alexis_D Apr 09 '15 at 22:36
1

Yes if this form is mapped with you User entity then you have to create a property or get method to do so. Otherwise you have to make the delete field as mapped false.

Imran
  • 4,582
  • 2
  • 18
  • 37