CakePHP:FormHelper不保存来自具有相同名称的两个输入的数据 [英] CakePHP: FormHelper not saving data from two inputs with same name

查看:116
本文介绍了CakePHP:FormHelper不保存来自具有相同名称的两个输入的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表单中有一长串复选框,我想显示为两列复选框(与演示文稿有关的原因)。所以在下面的代码中我将选项分成两个单独的数组,并用相同的名称创建两个不同的选项。当我 debug($ this-> request-> data); 'location'键总是空的。然而,相同的代码工作作为单个输入就好。我做错了什么?

Inside my form there is a long row of checkboxes which I want to show as two columns of checkboxes (for reasons related to presentation). So in the following code I split the options into two separate arrays and create two different options by the same name. When I debug($this->request->data); the 'location' key is always empty. However, the same code works as a single input just fine. What am I doing wrong?

<?php
$count = count($location_options); //$location_options is passed from the controller
$half = round( $count/2 );

$location_options1 = array_slice($location_options, 0, $half, TRUE);
$location_options2 = array_slice($location_options, $half, NULL, TRUE);

//I CAN'T GET THIS TO WORK!!
//echo $this->Form->input('location', array('type'=>'select', 'multiple'=>'checkbox', 'options'=>$location_options1, 'div'=>array('class'=>'col-xs-12 col-sm-6 form-group', 'style'=>'margin-bottom:0;', 'selected'=>$user_location_alert_tag_ids))); 
//echo $this->Form->input('location', array('type'=>'select', 'multiple'=>'checkbox', 'options'=>$location_options2, 'div'=>array('class'=>'col-xs-12 col-sm-6 form-group', 'selected'=>$user_location_alert_tag_ids))); 

//BUT THIS WORKS JUST FINE
echo $this->Form->input('location', array('type'=>'select', 'multiple'=>'checkbox', 'options'=>$location_options, 'div'=>array('selected'=>$user_location_alert_tag_ids))); 
?>


推荐答案

查看生成的HTML,

Look at the generated HTML, for each select element a hidden field is generated that ensures that the appropriate key will be present in the data.

具有相同名称的多个字段将导致生成多个隐藏字段,其中最后一个字段将覆盖前者。

Multiple fields with the same name will cause multiple hidden fields to be generated where the last one will overwrite the former ones.

这可以使用 hiddenField 选项 用于附加字段,使得仅针对第一输入生成隐藏初始化字段。从文档引用:

This can be avoided using the hiddenField option for the additional fields, so that the hidden initializer field is only generated for the first input. Quote from the docs:


如果要在表单上创建多个输入块,这些输入都分组在一起,则应使用此参数对所有输入除了第一个。如果隐藏输入在多个地方的页面上,则只会保存最后一组输入的值。

If you want to create multiple blocks of inputs on a form that are all grouped together, you should use this parameter on all inputs except the first. If the hidden input is on the page in multiple places, only the last group of input’s values will be saved.

两个输入的唯一ID,否则您将最终使用无效的HTML,因为帮助程序将产生重复的ID。

Also you should define a unique ID for both inputs, otherwise you'll end up with invalid HTML as the helper will produce duplicate IDs.

最后但并非最不重要的是,您的括号可能是一个错误, selected 键嵌套在 div 键中,我猜这是错误的所选项目。

And last but not least your parentheses are probably a little wrong, the selected key is nested in the div key, which I guess is wrong in case this is ment to define the selected entries.

echo $this->Form->input('location', array(
    'id'       => 'location1',
    'type'     => 'select',
    'multiple' => 'checkbox',
    'options'  => $location_options1,
    'div'      => array('class' => 'col-xs-12 col-sm-6 form-group', 'style'= > 'margin-bottom:0;')
    'selected' => $user_location_alert_tag_ids
))); 
echo $this->Form->input('location', array(
    'id'          => 'location2',
    'type'        => 'select',
    'multiple'    => 'checkbox',
    'options'     => $location_options2,
    'div'         => array('class' => 'col-xs-12 col-sm-6 form-group'),
    'selected'    => $user_location_alert_tag_ids
    'hiddenField' => false
)));

这篇关于CakePHP:FormHelper不保存来自具有相同名称的两个输入的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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