|
symfony sfFormでラベルと値を表示するwidget
2009年9月 4日(金) 11:47 JST
投稿者: 河口
sfFormで入力画面を作成する場合、各項目はwidgetを設定していきますが、 入力させたくないけど、画面には表示したいというケースは結構あると思います。
class myWidgetFormInputHiddenShowValue extends sfWidgetForm
{
protected function configure($options = array(), $attributes = array())
{
// デフォルトは'div'タグで表示
$this->addOption('tag', 'div');
$this->setOption('is_hidden', false);
}
public function render($name, $value = null, $attributes = array(), $errors = array())
{
// hiddenタグを作成
$hiddenTag = $this->renderTag('input', array('type' => 'hidden', 'name' =>$name, 'value'=>$value, 'class'=>null));
if($this->getOption('tag'))
{
// 値表示用タグを作成
$content = $this->renderContentTag($this->getOption('tag'), $value, $attributes);
} else {
// タグ指定がなければ設定値を表示
$content = $value;
}
return $content.$hiddenTag;
}
}
オプションの'tag'で表示する際に使用するタグを指定できるようにしています。 ○出力結果 (もし、デフォルトで出来るのであれば突っ込みお願いします) タグ: symfony |

