0

いくつかのカスタム入力データを取得するために、チュートリアルに従ってカスタム Zend_form_element を作成しました。すべてが多かれ少なかれ問題なく、正しく表示されます。私がする必要があるのは、更新のためにフォームを表示するとき、または検証に合格しないときにフォームを表示するときに、それを入力することです。カスタム要素のコードは次のとおりです。

class ZC_Form_Element_TabellaRendite
    extends Zend_Form_Element_Xhtml
{

    public $helper = "tabellaRenditeElement";
    private $_data;
    // the second paramater was added by me, i'll explain why below
    function __construct($spec, $data = null){
        $this->_data = $data;
        parent::__construct($spec);
    }

    public function setValue() {

    }

    public function getValue() {

        return $this->_data;
    }
}

そして、ここにヘルパー関数があります

    class ZC_View_Helper_TabellaRenditeElement
        extends Zend_View_Helper_FormElement
    {

        protected $html = '';

        public function tabellaRenditeElement ($name, $value=null, $attribs = null){
//Here the $attribs are correctly the $specs i passed, the $value only has some value because of the workaround i explain below
            $helper = new Zend_View_Helper_FormText();
            $helper->setView($this->view);
            fb($value, 'value in ');
            fb($name, 'name');
            $options = array('class'=> 'somma','size'=> 4);
            $optionsReadonly = array('readonly' => 1, 'class'=> 'totale', 'size'=> 4);
            if (!$attribs['modificabile']){
                $options['readonly'] = 1;
            }
            $this->html .= "
            <table class='display datatablesRendite' id='tableRendite' style='border:1px solid;'>
                        <thead>
                            <tr bgcolor='#B8D3E8'>
                                <th>RENDITA da LOCAZIONI (canone di locazione - manutenzione)</th>
                                <th>Importo</th>
                            </tr>
                        </thead>
                        <tbody>";
            $this->html .= '<tr>';
            $this->html .= '<td>LOCALI COMMERCIALI - IMPIANTI SPORTIVI</td>';
            $this->html .= '<td>';
            $this->html .= $helper->formText("renditaImpianti",$value['renditaImpianti'], $options);
            $this->html .= '</td>';
            $this->html .= '</tr>';
            $this->html .= '<tr>';
            $this->html .= '<td>LOCALI COMMERCIALI - AGGIUNTI (servizio di ristorazione)</td>';
            $this->html .= '<td>';
            $this->html .= $helper->formText("renditaAggiunte", $value['renditaAggiunte'], $options);
            $this->html .= '</td>';
            $this->html .= '</tr>';




            $this->html .= '</tbody></table>';


            return $this->html;
        }
    }

私は zend_framework にまったく慣れていませんが、これは明らかに間違っています。データと呼ばれる 2 番目のパラメーターを要素の __construct に追加しました。これを行ったのは、フォームを作成し、データを渡して入力するときに、ヘルパーに渡す方法がわかりません。そのため、データをコンストラクターのカスタム zend_form_element に直接渡すという回避策を作成しました (理由はわかりません)。

これは、もし私がそうするなら

$form = new My_Form();
$form->populate($data);

また

$form = new My_Form();
$form->isValid($_POST);

ヘルパーの $value が空です。

したがって、フォームの init() 関数では、次のように $data をカスタム要素に渡します。

$myCustomElement = new My_custom_element($specs, $data);

作成時にデータをフォームに渡します

$form = new My_Form($data);//this way i pass the data to populate custom elements
$form->populate($data);//this way i populate all the standard elements

isValid() も同様

$form = new My_Form($_POST);//this way i pass the data to populate custom elements
$form->isValid($_POST);//this way i populate all the standard elements

この方法ですべて問題なく動作しますが、これはかなり間違っていると確信しています。私の上司は最終的にコードをリファクタリングするために半日を与えたので、カスタム フィールドと標準フィールドの両方に $form->populate() と $ を入力したいと考えています。フォーム->isValid()。

PS 多分私はすべて間違っていて、これは私がやりたかったことを行う正しい方法ではありません: 正しい方法を指摘してください。私はフレームワークに不慣れで、完全に理解する時間がありませんでした.

4

1 に答える 1

2

populateに関する限り、ZC_Form_Element_TabellaRendite次のようにすれば十分だと思います。

class ZC_Form_Element_TabellaRendite extends Zend_Form_Element_Xhtml {

    public $helper = "tabellaRenditeElement";

    /**
    * Is the value provided valid?
    *
    *
    *@param  string $value
    *@param  mixed $context
    *@return bool
    */
     public function isValid($value, $context = null) {
       // you need to specify what it means that your element is valid or not.
     }

}

データの変数を作成する必要はありません。のメソッドZend_Form_Elementがこれを処理します。これにより、要素の値を次のように設定できます。

    $t = new ZC_Form_Element_TabellaRendite('somename', array('modificabile' =>'1'));
    $t->setValue($data);     

また、次のように、この要素をフォームに入力できるはずです。

    $data2Populate = array(
        'somename' => array(
            'renditaImpianti' => 112,
            'renditaAggiunte' => 132
        )
    );

    $myForm = new My_Form();
    $myForm->populate($data2Populate);

お役に立てれば。

于 2011-05-18T10:48:34.787 に答える