SfWidgetFormReadOnlyInput
Versione del 4 lug 2010 alle 13:21 di imported>Giorgio
Avevo bisogno di un widget in symfony che mi mettesse del testo fisso quando il campo è readonly.
Così ho creato questo.
Quando il campo è readonly crea un input hidden e scrive il valore in uno span formattato con i suoi attributi.
Un testo<hidden readonly="readonly" name="quest[name]" value="Un testo" id="quest_name" />
<?php
/**
* sfWidgetFormReadOnlyInput is an input widget with read only option that render a fix text forms
*
* @author Giorgio Borgonovo borgonovo [at] tavernadelleidee.it
*/
class sfWidgetFormReadOnlyInput extends sfWidgetForm
{
public function configure($options = array(), $attributes = array())
{
$this->addOption('format',"%s");
$this->addOption('default_value'," ");
parent::configure($options = array(), $attributes = array());
}
public function render($name, $value = null, $attributes = array(), $errors = array())
{
if ($this->getAttribute('readonly')=='readonly')
{
$attributes_reduced = $attributes;
unset($attributes_reduced['readonly'],$attributes_reduced['name']);
return "<span ".$this->attributesToHtml($attributes_reduced).">".sprintf($this->getOption('format'),($value?$value:($this->getOption('default_value'))))."</span>".$this->renderTag('hidden', array_merge(array('type' => $this->getOption('type'), 'name' => $name, 'value' => $value), $attributes));
} else {
return $this->renderTag('input', array_merge(array('type' => $this->getOption('type'), 'name' => $name, 'value' => $value), $attributes));
}
}
}
?>