dowm41315 2010-03-20 05:32
浏览 26
已采纳

Zend:如何将数据填充到复选框?

I am working on zend. I have a form with some checkboxes. I want to get data from database and populate this data to this form. If '1' is stored in table field then tick the check box otherwise leave it alone. In textboxes and dropdowns, data is easily populated but how to check a checkbox in action.

I am creating checkboxes and textboxes elements like this in form.php:

// Person name
$person = $this->CreateElement('text', 'name');
$person->setLabel('Name');
$elements[] = $person;    

// Organization name
$person = $this->CreateElement('text', 'organization');
$person->setLabel('Organization');
$elements[] = $person;       

// isAdmin Checkbox
$isAdmin = $this->CreateElement('checkbox', 'isAdmin');
$isAdmin->setLabel('Admin');
$elements[] = $isAdmin;

$this->addElements($elements);
$this->setElementDecorators(array('ViewHelper'));

// set form decorator (what script will render the form)
$this->setDecorators(array(array('ViewScript' , array('viewScript' => 'organization/accessroles-form.phtml'))));

How I render a form:

<form name="myform" action="<?= $this->element->getAction() ?>" method="<?= $this->element->getMethod() ?>" id="accessrolesform">

<table align="center" width="100%" border="0"><tbody>

<tr>
    <td><?= $this->element->name ?></td>
</tr>

<tr>
    <td><?= $this->element->organization ?></td>
</tr>


<tr>
    <td><?= $this->element->isAdmin ?></td>
</tr>

</table>
</form>

And populating data like this (for example):

// Prepare data to populate
$data['name'] = 'Naveed';
$data['organization'] = 'ABC';
$data['isAdmin'] = '1';

// Populate editable data
$this->view->form->populate( $data );

It is populating data in textboxes but not checking the checkbox? Any idea that how to check a checkbox from action?

Thanks

  • 写回答

2条回答 默认 最新

  • drexlz0623 2010-03-20 05:58
    关注

    From the manual:

    By default, the checked value is '1', and the unchecked value '0'. You can specify the values to use using the setCheckedValue() and setUncheckedValue() accessors, respectively. Additionally, setting the value sets the checked property of the checkbox. You can query this using isChecked() or simply accessing the property. Using the setChecked($flag) method will both set the state of the flag as well as set the appropriate checked or unchecked value in the element. Please use this method when setting the checked state of a checkbox element to ensure the value is set properly.

    $checkboxElement->setChecked(true);
    

    will set the value of the checkbox to 'checked="checked" with a value of "1".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角