Zend_Form いろいろ

11月 4th, 2009 by admin Leave a reply »

Zend_FormのDecoratorマジよくわからん。
少しずつなれてきたけど、まだ核心が掴めていない感じ。
とりあえずメモだー!

まずは復習、
未指定の場合、標準のDecorator

$this->addDecorator('FormElements')
     ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
     ->addDecorator('Form');

標準のElementDecorators

$this->addDecorator('ViewHelper')
    ->addDecorator('Errors')
    ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
    ->addDecorator('HtmlTag', array('tag' => 'dd'))
    ->addDecorator('Label', array('tag' => 'dt'));

標準のElementDecorators

$this->addDecorator('ViewHelper')
    ->addDecorator('Errors')
    ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
    ->addDecorator('HtmlTag', array('tag' => 'dd'))
    ->addDecorator('Label', array('tag' => 'dt'));

標準のDisplayGroupDecorators

$this->addDecorator('FormElements')
    ->addDecorator('HtmlTag', array('tag' => 'dl'))
    ->addDecorator('Fieldset')
    ->addDecorator('DtDdWrapper');

今回はグループ化したのを
・グループをDLで仕切りをして
・グループ内の各項目をTABLEで表示
・必須項目に(*)を追加
・フォームにDescriptionを入れる(各要素ではなくて)

まずは要素を作っていく

        $questSelect = array(
            'Disable'  => '使わない',
            'Enable'   => '使う'
        );
 
        $form = new Zend_Form();
        $form->setAction('')
            ->setMethod('post')
            ->setDescription('<span class="required">(*)</strong> は必須項目です。')
            //全般設定
            ->addElement( $form->createElement(
                'textarea', 'text_hoge',array(
                    'label'      => 'ほげ1',
                    'cols'       => 60,
                    'rows'       => 5,
                    'description'=> '注意事項など'
                 )))
            ->addElement( $form->createElement(
                'radio', 'select_hoge',array(
                    'label'        => '選択',
                    'MultiOptions' => $questSelect,
                    'required'     => true,
                 )))
            ->addDisplayGroup(
                array(
                    'text_hoge',
                    'select_hoge',
                ),
                'group1',
                array(
                    'Legend'=>'グループタイトル',
                    'description'=> 'グループの説明'
                )
            )
            ->addElement( 'button', '_saveButton',array(
                        'label'=>'保存',
                        'type'=>'submit'
                ))
            ->setElementFilters(array('StringTrim','StripTags'));
            //デザイン
            ->setDecorators(array(
                    'FormElements',
                    array('decorator' => 'Description', 'options' => array('tag' => 'p', 'class' => 'description','escape' => false)),
                    array('decorator' => 'HtmlTag', 'tag' => 'div' ,'options' => array('class'=>'zend_form')),
                    'Form',
                ))
            ->setDisplayGroupDecorators(array(
                    'FormElements', 
                     array('decorator' => 'Description', 'options' => array('tag' => 'p', 'class' => 'description','escape' => false)),
                     array('decorator' => 'HtmlTag', 'options' => array('tag' => 'table','width'=>'100%')),
                    'Fieldset',
                ))
            ->setElementDecorators(array(
                    'ViewHelper', 
                    'Errors',
                    array('decorator' => 'Description', 'options' => array('tag' => 'p', 'class' => 'description','escape' => false)),
                    array('decorator' => 'HtmlTag', 'options' => array('tag' => 'td')),
                    array('decorator' => 'Label', 'options' => array('tag' => 'th','requiredSuffix'=>'<span style="color:red">(※)</span>','escape' => false)),
                    array('decorator' => array('OuterHtmlTag' => 'HtmlTag'), 'options' => array('tag' => 'tr')),
                ))
 
            ->_saveButton->setDecorators(array(  
                   'ViewHelper',  
                   array('decorator' => array('td' => 'HtmlTag'),'options' => array('tag' => 'td', 'colspan' => 2 , 'class'=>'td_submit' )),  
                   array('decorator' => array('tr' => 'HtmlTag'),'options' => array('tag' => 'tr')),

これを使った出力とスタイルシートを入れたサンプルを後日追加予定。
まずはここまでメモ。

Advertisement

コメントを残す