Zend_Form_decorator Tableで表示

10月 13th, 2009 by admin Leave a reply »

Zend_formを頻繁に使うけど、いまいちdecorator周りがつかめないので
出来上がってるところからメモ。
タイトルとやっていることがちょっと違うような気がしないでもないが、そこはご愛嬌。

公式のドキュメントを何度も読み返しているけど、難しい・・。
やりたいことがいまいち出来てないけど、とりあえずってことで
以下にだいたいの流れをメモ。

呼び出し元のアクションコントローラー

    public function inputAction()
    {
        $dbHoge = new DbTable_hoge();
        if (!$rowHoge = $dbHoge->find( $hoge )->current() ){
            $rowHoge = $dbHoge->createRow();
 
            //デフォルト
            $rowHoge->type = '1';
        }
 
 
        $form = $this->_Form();
        if ($_POST) {
            if ($form->isValid( $_POST )) {
 
                $rowHoge->setFromArray( $form->getValues() );
                $rowHoge->save();
 
                //保存後の処理
 
                exit();
            }
 
        }else{
            $form->setDefaults( $rowHoge->toArray() );
        }
 
        return $form->render(new Zend_View);
}

Form部分。

    private function _Form()
    {
 
        $_select = array(
            '0' => 'パターン1',
            '1' => 'パターン2',
            '2' => 'パターン3',
        );
 
 
        $form = new Zend_Form();
        $form->setAction('')
            ->setMethod('post')
            ->addElement( $form->createElement(
                'radio', 'type',array(
                    'label'        => '選択タイプ',
                    'MultiOptions' => $_select,
                    'required'     => true
                 )))
            ->addElement( $form->createElement(
                'text', 'list',array(
                    'label'      => 'リスト数',
                    'size'       => '3',
                    'required'   => true,
                    'description'=> '数字を入れる場所',
                    'validators' => array(
                        array('int')
                    ),
                 )))
            ->addElement( $form->createElement(
                'checkbox', 'view',array(
                    'label'      => 'チェックボックス',
                 )))
            ->addElement('button', '_save', array('label' => ' 保存 ', 'type'=>'submit'))
            ->setElementFilters(array('StringTrim','StripTags'));
 
        //テーブル表示
        $form->setDecorators(array(
                'FormElements', 
                    array('decorator' => 'HtmlTag', 'options' => array('tag' => 'table', 'class' => 'zend_form','width'=>'100%')),
                    'Form',
                ))
            ->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')),
                    array('decorator' => array('OuterHtmlTag' => 'HtmlTag'), 'options' => array('tag' => 'tr')),
                ))
            ->_save->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')),  
                )); 
 
        return $form;
 
    }

使いまわしするときは
setDecoratorsとsetElementDecoratorの中身を
外部でprotectedでもしておいて呼ぶ。

必須項目のthにクラス名を入れたりしたいんだけど
はっきり申してよくわからん。
また追々調べていこう。

Advertisement

コメントを残す