开发者

How to populate a custom zend_form_element with $form->populate() and $form->isValid()

I've created a custom Zend_form_element following a tutorial to have some custom input data. Everything is more or less okay, it displays correctly and so on. What i need to do is to populate it when i display the form for updating or when displaying the form when it does not pass validation. Here is the code for my custom 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;
    }
}

And here is the helper function

    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;
        }
    }

I'm totally new to the zend_framework and this is obviously wron, as you se i added a second parameter called data to the __construct of the element: i've done this because when i create my form and pass the data to populate it i don't know how to have it passed to the helper. So i made the workaround of passing the data directly to the custom zend_form_element in the constructor and (i don't know why) it works.

This means that if i do

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

or

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

The $value in the helper is empty.

So in the init() function of the form i pass the $data to the custom element like this:

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

and i pass the data to the form on creation

$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

Same for 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

This way everythin开发者_高级运维gs work ok, but i'm sure this is pretty wrong: my boss finally gave me half a day to refactor the code and so i want to poulate bothe the custom and standard fields with $form->populate() and $form->isValid().

P.S. maybe i got everything wrong and this is not the correct way to do what i wanted to do: feel free to point out the correct way, i'm new to the framework and i i hadn't the time to understand it fully.


I think that, as far as populate is concerned, what should be enough is to have ZC_Form_Element_TabellaRendite as follows:

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.
     }

}

You don't need to create any variables for your data. Methods from Zend_Form_Element will take care of this. With this you can set the element value as, e.g.:

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

Also you should be able to populate the form with this element as, e.g.:

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

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

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜