0

我正在处理现有项目。有很多这样的形式:

<?php echo $form->create('MyForm', array('id' => 'MyFormId')); ?>

  <?php echo $form->input('User.0.username', array( 'label' => false, )); ?>
  <?php echo $form->input('User.0.location', array( 'label' => false, )); ?>

<?php echo $form->end(); ?>

它正在生成这样的表单元素:

<input type="text" id="User0Username" name="data[User][0][username]">
<input type="text" id="User0Location" name="data[User][0][location]">

但我希望它们看起来像这样:

<input type="text" id="User0Username" name="User_0_username">
<input type="text" id="User0Location" name="User_0_location">

是否有任何$form->create();功能选项来获取此 html 而不是更改我的表单元素?

谢谢

4

2 回答 2

1
The Form helper is pretty smart. Whenever you specify a field name with the form helper methods, it'll automatically use the current model name to build an input with a format like the following:

    <input type="text" id="ModelnameFieldname" name="data[Modelname][fieldname]">

您可以通过传入 Modelname.fieldname 作为第一个参数来手动指定模型名称。

echo $this->Form->input('Modelname.fieldname');

如果您需要使用相同的字段名称指定多个字段,从而创建一个可以使用 saveAll() 一次性保存的数组,请使用以下约定:

<?php
echo $this->Form->input('Modelname.0.fieldname');
echo $this->Form->input('Modelname.1.fieldname');
?>

<input type="text" id="Modelname0Fieldname" name="data[Modelname][0][fieldname]">
<input type="text" id="Modelname1Fieldname" name="data[Modelname][1][fieldname]">
于 2012-09-04T09:40:10.803 回答
0

我总是使用括号版本 - 即使是 jquery ajax 提交。顺便说一句,它是这样工作的:

var ids = new Array();
var myCheckboxes = new Array();
$("#ProductCalculation input:checked").each(function() {
    ids.push($(this).val());
});
...
$.ajax({
    dataType: 'json',
    type: "post",
    data: {
        'data[Product][product_calculation_id]': ids, 
        'data[Product][brand_id]': brands, 
        'data[Category][Category]': categories, 
        'data[Form][connect]': c
    },
    url: targeturl,
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    },
    success: function(html) {
        ...
    }
});

您可以手动抓取表单元素。不理想,但它不会破坏蛋糕式编码

我也确信可以编写一个 jquery 插件以某种方式自动实现这一点。这实际上是一件非常有趣的事情。

于 2012-09-04T09:57:05.447 回答