Anyway I used copied the Symfony code for objects_for_select and made
my own helper out of it. Its like this...

/**
 * Accepts a container of objects, the method name to use for the
value,
 * and the method name to use for the display.
 * It returns a string of option tags.
 *
 * Default text to be displayed in the drop-down list can be passed.
 * $defaultValue: value of default option
 * $defaultText: display text of default option
 *
 * NOTE: Only the option tags are returned, you have to wrap this call
in a regular HTML select tag.
 */
function custom_objects_for_select($options, $value_method,
$text_method = null, $selected = null, $defaultValue = null,
$defaultText = null, $html_options = array())
{
  $select_options = array();

 // Added the following if condition
  if($defaultValue != null && $defaultText != null)
  {
    $select_options[$defaultValue] = $defaultText;
  }

  foreach ($options as $option)
  {
    // text method exists?
    $method_exists = ($text_method == '__toString') ? method_exists
($option, $text_method) : is_callable(array($option, $text_method));
    if ($text_method && !$method_exists)
    {
      throw new sfViewException(sprintf('Method "%s" doesn\'t exist
for object of class "%s".', $text_method, _get_class_decorated
($option)));
    }

    // value method exists?
    $method_exists = ($value_method == '__toString') ? method_exists
($option, $value_method) : is_callable(array($option, $value_method));
    if (!$method_exists)
    {
      throw new sfViewException(sprintf('Method "%s" doesn\'t exist
for object of class "%s".', $value_method, _get_class_decorated
($option)));
    }

    $value = $option->$value_method();
    $key = ($text_method != null) ? $option->$text_method() : $value;

    $select_options[$value] = $key;
  }

  return options_for_select($select_options, $selected,
$html_options);
}



Thanks and Regards
Vikram

On Feb 25, 4:13 pm, SeeVik <[email protected]> wrote:
> Hello all,
>
> I am using the object_for_select helper to display options in my drop-
> down list.
>
> <?php echo select_tag(
>         'items',
>         objects_for_select(
>           $list,
>           'getItemId',
>           'getItemName'
>         )
>       ) ?>
>
> Now the list is getting the values from the the object $list. But in
> this list, I want a string like 'Select an item...' to be displayed as
> the first option. Should I create my own helper for such a task? or is
> there some other way?
>
> Thanks and Regards
> Vikram
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to