如何使用$ form-> populate()和$ form->填充自定义zend_form_element isValid()

时间:2011-05-18 08:16:54

标签: php zend-framework zend-form zend-form-element

我在教程后创建了一个自定义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完全不熟悉,这显然是wron,因为你向元素的__construct添加了第二个名为data的参数:我这样做是因为当我创建表单并将数据传递给填充它我不知道如何将它传递给帮助者。所以我做了解决方法,将数据直接传递给构造函数中的自定义zend_form_element,并且(我不知道为什么)它的工作原理。

这意味着如果我这样做

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

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

帮助程序中的$值为空。

因此,在表单的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-&gt;填充自定义和标准字段()和$ form-&gt; isValid()。

P.S。也许我把一切都搞错了,这不是我想做的正确方法:随意指出正确的方法,我是框架的新手,而没有时间完全理解它。

1 个答案:

答案 0 :(得分:2)

我认为,就人口而言,应该是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);

希望这有帮助。

相关问题