HTML_QuickFormのグループ化されたエレメントを確認画面でスマートに表示する
HTML_QuickFormで良く使うradioやchexboxのグループ化
便利なんだけど,確認画面でいったんremoveElementして,さらにaddElement(hidden)しなおさないと確認表示が妙?な感じなる
たぶん上記のやり方がはやいけど,
ライブラリを全く触ったことがなかったのでこれを機会に触ってみた(今回はradioの方)
ディレクトリの構成はPEARからダウンロードしてきたものを想定
ex_radio.php
<?php require_once 'HTML/QuickForm/radio.php'; class HTML_QuickForm_ex_radio extends HTML_QuickForm_radio { /** * Class constructor * * @param string Input field name attribute * @param mixed Label(s) for a field * @param string Text to display near the radio * @param string Input field value * @param mixed Either a typical HTML attribute string or an associative array * @since 1.0 * @access public * @return void */ function HTML_QuickForm_ex_radio($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null) { parent::HTML_QuickForm_radio($elementName, $elementLabel, $text, $value, $attributes); } /** * Returns the radio element in HTML * * @since 1.0 * @access public * @return string */ function toHtml() { if (0 == strlen($this->_text)) { $label = ''; } elseif ($this->_flagFrozen) { if($this->_attributes['checked']) { $label = $this->_text; } } else { $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>'; } return HTML_QuickForm_input::toHtml() . $label; } /** * Returns the value of field without HTML tags * * @since 1.0 * @access public * @return string */ function getFrozenHtml() { if ($this->getChecked()) { return $this->_getPersistantData(); } } } ?>
Render/Default.php(これは人それぞれ)のfinishGroupを触る
<?php /** * 色々な関数があります */ /** * Called when visiting a group, after processing all group elements * * @param HTML_QuickForm_group group being visited * @access public * @return void */ function finishGroup(&$group) { $separator = $group->_separator; if(!$group->_flagFrozen) { if (is_array($separator)) { $count = count($separator); $html = ''; for ($i = 0; $i < count($this->_groupElements); $i++) { $html .= (0 == $i? '': $separator[($i - 1) % $count]) . $this->_groupElements[$i]; } } else { if (is_null($separator)) { $separator = ' '; } $html = implode((string)$separator, $this->_groupElements); } } else { $html = implode('', $this->_groupElements); } if (!empty($this->_groupWrap)) { $html = str_replace('{content}', $html, $this->_groupWrap); } $this->_html .= str_replace('{element}', $html, $this->_groupTemplate); $this->_inGroup = false; } /** * 色々な関数があります */ ?>
checkboxもこんな感じで拡張すればできるはず
ソースを載せてるんですがそんなに変わってないYO