Symfony小部件清除标签以编程方式呈现

时间:2015-02-19 11:05:44

标签: php symfony-1.4

我正在尝试以编程方式在执行某些检查后设置窗口小部件的标签值,但我的以下尝试无法正常工作。谁能看到我做错了什么?请注意,标签已有值,我只想清除它。那是在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
}

1 个答案:

答案 0 :(得分:0)

在渲染方法中调用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);
}