tags:

views:

386

answers:

1

I am running Symfony 1.2 and utilizing the object helper to create some inline editable fields outside of a form. Because there is no symfony or scriptalicious short cut to create an inline edit tag for multiple choice select boxes (much like input_in_place_editor_tag or Ajax.InPlaceCollectionEditor), I am using object_select_tag with the multiple option set to true to create a select box like so:

<?php      
  echo object_select_tag($user->getsfGuardPermissions(), 'getId', array (
 'include_blank' => false,
 'related_class' => 'sfGuardPermission',
 'peer_method' => 'doSelect',
 'text_method' => '__toString',
 'control_name' => 'user_customer_permissions_'.$user_customer->getId(),
     'multiple' => true
));
?>

I will then call observe_field to update the object via Ajax when the form is changed. However

The problem is that while $user->getsfGuardPermissions() returns an array of sfGuardPermission objects. They are not selected by default.

After some testing, I found that if you pass it only one sfGuardPermission object instead of an array, the proper option is selected. Is this a limitation of Symfony or is there a different way of doing this? It's hard to believe that symfony would include the 'multiple' option if they didn't allow you to select multiple options by default.

Looking at ObjectHelper.php it is apparent that the function is not expecting an array. How do I select multiple objects by default?

+1  A: 

object___select ___tag() will accept an integer array corresponding to the object values you want selected. For example:

<?php
  echo object_select_tag(array(4, 5), 'getId', array (
 'include_blank' => false,
 'related_class' => 'sfGuardPermission',
 'peer_method' => 'doSelect',
 'text_method' => '__toString',
 'control_name' => 'user_customer_permissions_'.$user_customer->getId(),
     'multiple' => true
));
  ?>

Will select values 4 and 5 by default. This is not ideal, as the documentation lists the first parameter as $object object select tag (symfony API). So I suppose this can be considered a workaround. Ideally the function would accept an array of objects for the $object parameter when the multiple flag is set to true. This would be consistent with other helper functions.

markb