[英]CakePHP does not read the correct input names in a cloned form generated by javascript
我实现了扩展表单功能,该功能还可以在克隆表单时生成唯一的字段名称。
由于js的工作方式,扩展表单库(#readroot)中的字段名称与Cake不兼容,直到运行脚本并对其进行更正。
基地名称: 年龄,年级,学校
js运行后的样子:
data[Student][1][age]
data[Student][1][grade]
data[Student][1][school]
所以我希望$this->request->data
会收到这样的数组:
Array (
[Post] => Array (
[title] => Mr
[contact_person] => Sam
[home_tel] => 1234567
[mobile] => 1234567
[email] => email@gmail.com
[relationship] => family
[frequency] => once per week
[duration] => 1hr
[user_id] => 1
)
[Student] => Array (
[0] => Array (
[age] => 10
[grade] => 1
[gender] => 1
[school] => ABC
)
// This is where the extend form starts.
[1] => Array (
[age] => 11
[grade] => 2
[gender] => 0
[school] => ABC2
)
)
)
但是, $this->request->data
读取以下内容:
Array (
[age] => 11
[grade] => 2
[gender] => 0
[school] => ABC2
[Post] => Array (
[title] => Mr
[contact_person] => Sam
[home_tel] => 1234567
[mobile] => 1234567
[email] => email@gmail.com
[relationship] => family
[frequency] => once per week
[duration] => 1hr
[user_id] => 1
)
[Student] => Array (
[0] => Array (
[age] => 10
[grade] => 1
[gender] => 1
[school] => ABC
)
) )
看起来它在更改之前先读取了字段名称。 我是Java语言的新手,希望有人能救我...
表单html和javascript的简化版本:
<?php
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('contact_person');
echo $this->Form->input('home_tel');
echo $this->Form->input('mobile');
echo $this->Form->input('email');
echo $this->Form->input('relationship');
echo $this->Form->input('Student.0.age');
echo $this->Form->input('Student.0.grade');
echo $this->Form->input('Student.0.school');
?>
<!-- Extend Form Reference -->
<span id="readroot" style="display: none">
<input class="btn btn-default" type="button" value="Remove review" onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br /><br />
<?php
echo $this->Form->input('Student.1.age', array(
'name' => 'age',
);
echo $this->Form->input('Student.1.grade', array(
'name' => 'grade',
'options' => array(
'1' => __('Grade 1'),
'2' => __('Grade 2')
)
));
echo $this->Form->input('Student.1.school', array(
'name' => 'school'
));
?>
</span>
<!-- Extend Form Reference End-->
<span id="writeroot"></span>
<input class="btn btn-default" type="button" onclick="moreFields()" value="Give me more fields!" />
<script>
var counter = 1;
function moreFields() {
counter++;
var newField = document.getElementById('readroot').cloneNode(true);
newField.id = '';
newField.style.display = 'block';
var newFields = newField.querySelectorAll('[name]');
for (var i=0;i<newFields.length;i++) {
var theNames = newFields[i].name
if (theNames)
newFields[i].name = "data[Student][" + counter + "][" + theNames + "]";
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newField,insertHere);
}
//window.onload = moreFields;
</script>
<?php echo $this->Form->end(); ?>
由于我设法使其无法正常工作,因此我将其发布为答案。
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
$this->request->data['Post']['user_id'] = AuthComponent::user('id');
unset($this->Post->Student->validate['post_id']);
$a = $this->request->data;
$b = array_splice($a, 4);
if ($this->Post->saveAssociated($b)) {
$this->Session->setFlash(__('The post has been saved.'), 'alert_box', array('class' => 'alert-success'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.'), 'alert_box', array('class' => 'alert-danger'));
}
}
$users = $this->Post->User->find('list', array('order' => array('User.id' => 'asc')));
$subjects = $this->Post->Subject->find('list');
$this->set(compact('users', 'subjects'));
}
前面有4个值,而数组的其余部分正是我想要的,我基本上只是将其截断,然后将其余部分传递给保存。
好吧,因为您用除了Student.1.grade
以外的其他东西覆盖了data属性(在您的情况下为grade
)。
因此,Cake表单助手将其添加到数组的根目录而不是School密钥。
您的代码应如下所示:
echo $this->Form->input('Student.1.grade', array(
'name' => 'Student.1.grade',
'options' => array(
'1' => __('Grade 1'),
'2' => __('Grade 2')
)
));
要么
echo $this->Form->input('Student.1.grade', array(
'options' => array(
'1' => __('Grade 1'),
'2' => __('Grade 2')
)
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.