Symfony sfWidgetFormSelect with disabled options
2010-05-13The sfWidgetFormSelect doesn’t provide ability to render disabled options. It’s rarely used feature of a HTML select element, but sometimes it could save your life 🙂 In fact, we can achieve this feature by creating our own widget, like one listed below, which inherits from sfWidgetFormSelect. This solution is inspired by problem posted on Symfony Experts.
/** * Select widget with disabled options for values below zero * * @uses sfWidgetFormSelect * @author Wojciech Sznapka */ class myWidgetFormSelect extends sfWidgetFormSelect { /** * Overriden renderContentTag - if it's option then we check for disableCondition and set disabled attribute * * @see sfWidget::renderContentTag */ public function renderContentTag($tag, $content = null, $attributes = array()) { if ($tag == 'option' && isset($attributes['value']) && $this->disableCondition($attributes['value'])) { $attributes['disabled'] = 'disabled'; } return parent::renderContentTag($tag, $content, $attributes); } /** * Check if we should disable option depends on it vaule (value is below zero) * * @param mixed $value value of the option * @return boolean if true, the option will be disalbed */ protected function disableCondition($value) { return (int)$value < 0; } } ~