I've tried to find some documentation for sfWidgetFormDoctrineChoiceGrouped (within sfFormExtraPlugin), but there doesn't seem to be anything I can find on Google.
What I'm trying to do is create a dropdown menu on my form where a user can update the category_id field of the Page object with optgroups to organise categories > subcategories. The Category table is a Doctrine NestedSet with only two levels. I added a parent_id field so that I could use the group_by method of sfWidgetFormDoctrineChoiceGrouped but it doesn't format correctly.
$this->widgetSchema['category_id'] = new sfWidgetFormDoctrineChoiceGrouped(array('group_by' => 'parent_id', 'method' => 'getName', 'model' => 'Category'));
This works (sort of) but it re-displays the categories wrongly – see below for the output –
<select name="page[category_id]" id="page_category_id">
<option value="11">Cat 1</option>
<option value="24">Cat 2</option>
<option value="28">Cat 3</option>
<option value="33">Cat 4</option>
<option value="37">Cat 5</option>
<option value="44">Cat 6</option>
<option value="51">Cat 7</option></optgroup>
<optgroup label="1"><option value="2">New category</option>
<option value="3">Cat 1</option>
<option value="4">Cat 2</option>
<option value="5">Cat 3</option>
<option value="6">Cat 4</option>
Any help you can provide would be great!
Florian Klein answers:
It could be the ordering of your choices:
try to define a different order by using the "order_by" option of your widget:
'order_by' => array('root_id, lft',''),
You can also change the "method" option:
'method' => 'getIndentedName'
You just have to implement the getIndentedName method in your model object: ( Category )
public function getIndentedName()
return str_repeat('- ',$this['level']) . $this;
Actually, this method works for sfWidgetFormDoctrineChoice.
Tom Clark comments:
This is almost right, but it's nearly the same as what I had before. The problem is that the ID of the parent category is displayed, instead of the Name. Please see the screenshot – if you can tell me how I display the name instead of the ID, we'll be cooking on gas!