php - 禅德 : How to populate data to checkboxes?

标签 php zend-framework checkbox populate

我正在使用 zend。我有一个带有一些复选框的表单。我想从数据库获取数据并将这些数据填充到此表单中。如果“1”存储在表字段中,则勾选该复选框,否则将其保留。在文本框和下拉列表中,数据很容易填充,但如何检查操作中的复选框。

我正在 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'))));

如何呈现表单:

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

并像这样填充数据(例如):

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

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

它正在文本框中填充数据但不检查复选框?知道如何从操作中选中复选框吗?

谢谢

最佳答案

来自手册:

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

会将复选框的值设置为“checked=”checked”,值为“1”。

关于php - 禅德 : How to populate data to checkboxes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2482073/

相关文章:

c# - php 和 c# 中的 hmac_sha256 不同

php - 使用 Codeigniter 循环遍历此 json 对象 (jquery)

javascript - 返回 JSON 响应

JavaScript-多个复选框验证不起作用

wpf - 数据网格复选框自动化

php - Twig 过滤器/扩展中的 Symfony2 : How to get container parameters from parameters. yml?

php - 为选择元素设置占位符

javascript - ZF 1.9 从前端 html 重定向

php - Zend Framework 多个模块与管理

c# - 如何计算 asp.net 表单中的复选框