Symfony小部件在渲染时以编程方式清除标签


Symfony widget clear label programmatically on render

在执行了一些检查后,我试图以编程方式设置小部件的标签值,但以下尝试无效。有人看到我做错了什么吗?请注意,标签已经有了一个值,我只想清除它。它在symfony 1.4中。

class customFormSome extends sfWidgetFormTextarea {
/**
 * Constructor.
 *
 * @see sfWidgetFormTextarea
 * */
protected function configure($options = array(), $attributes = array()) {        
    $this->addOption('element_id');
}
/**
 * @param  string $name        The element name
 * @param  string $value       The value displayed in this widget
 * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
 * @param  array  $errors      An array of errors for the field
 *
 * @see sfWidget
 * */
public function render($name, $value = null, $attributes = array(), $errors = array()) {
   /*** SOME PROCESSING HERE******/
   $this->setAttribute('label', '');  //---->DOESNT WORK
   $this->setAttribute('label', FALSE);  //---->DOESNT WORK
   $this->setAttribute('label', NULL);  //---->DOESNT WORK
   $fields = $this->parent->setLabel($this->getOption('element_id'), '');//---->DOESNT WORK
}

在render方法中调用setAttiribute()为时已晚,它从$attributes参数中获取属性,所以只需要覆盖它:

public function render($name, $value = null, $attributes = array(), $errors = array()) {
   /*** SOME PROCESSING HERE******/
  $attributes['label'] = $this->getOption('element_id');
  return parent::render($name, $value, $attributes, $errors);
}