views:

162

answers:

2

I have a two Symfony forms:

ShoppingListForm ShoppingListItemForm

I'm embedding the ShoppingListItemForm inside the ShoppingListForm many times. ie A shopping list contains many items.

So the ShoppingListItemForm consists of two widgets:

item_id (checkbox) shopping_list_id (hidden - foreign key)

What I would like to do is delete the corresponding ShoppingListItem object if the object exists and the checkbox is left unchecked.

I'm not sure how this delete would occur? Would I use a post validator to see which fields have/haven't been checked? I'm a bit lost on this one.

Thanks in advance.

A: 

Hello, personnally, I would loop through the existing list items to see whether the corresponding checkboxes are checked in the action, and call the delete() method on the items for which it is not the case. I don't think it is the purpose of a post validator, I would do this directly in the action.

greg0ire
A: 

I'd do this by over-riding the ShoppingListForm's updateObject method and putting your custom delete() etc calls in there (be sure to call parent::updateObject() within it).

Depending how you implement it, you may also need to remove the embedded forms and their values to ensure saving still works correctly for the remaining objects. Try without, but if you do, you need to clear the following:

unset($taintedValues['ShoppingListItem'][$key]);
unset($this->embeddedForms['ShoppingListItem'][$key]);
unset($this->validatorSchema['ShoppingListItem'][$key]);
unset($taintedFiles['ShoppingListItem'][$key]);

If you want to see a custom updateObject method to get an idea how to interact with values etc:

http://www.symfony-project.org/forms/1_2/en/11-Doctrine-Integration#chapter_11_sub_customizing_the_updateobject_method

benlumley