开发者

Populate values to checkbox while editing in zend framework

I am trying to populate the values into check box. I want check box to be checked when there is value stored in database.

This is my code in form:

$form ['test_1'] = new Zend_Form_Element_Checkbox('test_1');
$form['test_1']->setLabel('test1')->setChe开发者_Python百科ckedValue('1');

$form ['test_2'] = new Zend_Form_Element_Checkbox('test_2');
$form['test_2']->setLabel('test2')->setCheckedValue('2');

If there is value 1 in database i want first check box to be checked and if its 2 then 2nd checkbox needs to be checked.

What do i need to do in the controller.

Could anyone please help me on this issue.


The easiest way would be to fetch the values from the database as an array that maps to the form input elements, e.g. return a row like

array('test_1' => 'value of checkbox', 'test_2' => 'value of checkbox');

You could then simply call $form->populate($values) and let do Zend_Form do the setting, e.g. in your controller do

public function showFormAction()
{
    $form = $this->getHelper('forms')->get('MyForm');
    $data = $this->getHelper('dbGateway')->get('SomeTable');
    $form->populate($data->getFormData());
    $this->view->form = $form;
}

Note: the helpers above do not exist. They are just to illustrate how you could approach this. Keep in mind that you want thin controllers and fat models, so you should not create the form inside the controller, nor put any queries in there.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜