0

我需要帮助

我有 2 个模型:律师和权力。

地点:AttorneyHABTMPowers

当我添加一个新的律师时,我选择了许多与权力相对应的复选框。

我的问题是:当我编辑律师时,如何使复选框显示为选中状态?他们没有。

edit.ctp 部分

<div class="control-group">
<label class="control-label">Poderes:</label>
<div class="controls">
<?php foreach ($poderes as $power):  ?>
<div class="checkbox tooltip" title="<?php echo $power['Power']['texto'] ?>">
<input type="checkbox" value="<?php echo $power['Power']['id'] ?>"  />
<label><?php echo $power['Power']['resumo'] ?></label>
</div>

我的编辑控制器。

 function edit($id = null) {


$this->set('poderes',$this->Attorney->Power->find('all'));
$this->Attorney->id = $id;

     if ($this->request->is('get')) {
        $this->request->data = $this->Attorney->read();

    } else {

        if ($this->Attorney->save($this->request->data) {


        $this->Session->setFlash('Usuário editado com sucesso!', 'default', array('class' => 'flash_sucess'));
        $this->redirect(array('action' => 'pesquisar'));
}
}

编辑:使用结果的图像$this->Form->input('Power', array('checkbox' => 'multiple'));

第一个答案的结果

4

1 回答 1

3

您应该正确使用 Form 助手,然后您需要做的就是读取Powers 的列表,并checkbox作为multiple选项的值传递给FormHelper::input(). CakePHP 将完成剩下的工作并检查相应的复选框。

http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::输入
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select

此外,您应该遵循 CakePHPs 命名约定,并为模型/(视图)变量名称等使用英文,即不要使用poderes,使用适当的复数形式Power,即powers.

控制器

...
$this->set('powers', $this->Attorney->Power->find('list'));
$this->Attorney->id = $id;
...

注意Model::find('list')需要正确使用Model::$displayField

看法

...
echo $this->Form->input('Power', array('multiple' => 'checkbox'));
...
于 2013-09-05T18:36:44.783 回答