CakePHP多个复选框数组HTML正确的方法 [英] CakePHP multiple checkbox array HTML the right way

查看:69
本文介绍了CakePHP多个复选框数组HTML正确的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CakePHP的复选框的表单生成器...当将以下内容传递给名称时:

CakePHP's form generator for checkboxes ... when passing the following into the name:

<?php echo $this->Form->checkbox('checkList[]', array( 'value'=>1,'id' => 'holiday'.$holidaysDays['id'], 'error' => false, 'placeholder' => false,'div'=>false,'label'=>false,'class' => 'approveHolidayCheckbox', 'data-off-text'=>'No', 'data-on-text' =>'Yes', 'hiddenField'=>true) ); ?>

输出:

<input type="checkbox" name="data[HolidaysApproval][checkList[]]" value="1" id="holiday238" class="approveHolidayCheckbox" data-off-text="No" data-on-text="Yes">

我在这里阅读: http://network-13.com/thread/3647-Creating-checkbox-arrays-with-CakePHP 字段名称的句号(如下所示),页面上将输出多个复选框。这是这样做的正确方法吗?

I read here:http://network-13.com/thread/3647-Creating-checkbox-arrays-with-CakePHP that the solution is adding a full stop to the field name (as below), where multiple checkboxes are output on the page. Is this the 'right' way to do this?

在文档的任何位置都看不到这种特殊情况。

Couldn't see this particular scenario anywhere in the documentation.

<?php echo $this->Form->checkbox('checkList.', array( 'value'=>1,'id' => 'holiday'.$holidaysDays['id'], 'error' => false, 'placeholder' => false,'div'=>false,'label'=>false,'class' => 'approveHolidayCheckbox', 'data-off-text'=>'No', 'data-on-text' =>'Yes', 'hiddenField'=>true) ); ?>


推荐答案

是的,这是正确的方法。当CakePHP构建字段名称时,它使用PHP的 explode()方法。因此,清单。基本上执行以下操作:-

Yes this is the correct way of doing this. When CakePHP builds the field names it uses PHP's explode() method. So checklist. essentially does the following:-

$fieldname = explode('.', 'checklist.');

其结果是:-

Array
(
    [0] => checkList
    [1] => 
)

所以您将获得名称为 data [Model] [checklist] [] 的输入。

So you would get inputs with the name data[Model][checklist][].

您可以类似地将其用于 hasMany 之类的字段,例如 eg $ this-> Form-> field('Model..name'); ,它将为您提供名称为 data [Model] [] [name ]

You can similarly use this for hasMany like fields, e.g. $this->Form->field('Model..name'); which would give you inputs with the name data[Model][][name].

看看FormHelper,您应该很容易看到它如何构建字段名称。

Take a look at the FormHelper and you should easily be able to see how it builds the field names.

这篇关于CakePHP多个复选框数组HTML正确的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆