我有一个场景,其中我有两个模型,个人资料和教育,以及个人资料和多种教育关系。
然后我有一个视图,我希望有多种形式:
这里的问题是我希望编辑表单填充每个条目的数据。 关于如何实现这一点的任何想法?
CakePHP文档声明:
FormHelper使用$ this-> request->数据属性自动检测是否创建添加或编辑表单。如果$ this-> request->数据包含以表单模型命名的数组元素,并且该数组包含模型主键的非空值,那么FormHelper将为该记录创建一个编辑表单
我已成功实施了一个编辑表单,但在我只有一个表单的情况下。
现在我有这样的事情:
EducationController.php
public class EducationController extends AppController{
/* (...) */
public function edit_index(){
$user_id = $this->Auth->user('id');
$profile = $this->Profile->find('first', array('conditions' => array('Profile.user_id' => $user_id)));
$this->set('profile', $profile);
$this->request->data = $profile;
}
/* (...) */
}
至于视图文件......
<?php
foreach ($profile['Education'] as $key => $value) {
?>
<div class="history-sub-menu" id="history-1">
<p> <?php echo $value['education'] . ' - ' . $value['school'] ?> </p>
<?php echo $this->Html->image("arrow_bot.png") ?>
</div>
<?php
/*
* Prints the form for editing... the id of the education entry being edited goes
* as a parameter
*/
echo $this->element('education_edit_form', array('hidden' => true, 'id' => $value['id']));
?>
<?php
}
?>
<div class="add-sub-menu">
<p><?php echo $add_another_position ?></p>
<?php echo $this->Html->image("plus_blue.png") ?>
</div>
<?php
/*
* Prints the form for adding... as no parameter is given as the id
* the form assumes it is a form for adding.
*/
echo $this->element('education_edit_form', array('hidden' => true));
?>
&#39; education_edit_form&#39;元素:
<?php
/* Prints a form for creating or editing an user academic entry.
*
* In case of edit form a variable id must be set with the identifier of
* the academic entry being creted.
*
* Other Paramaters:
* hiddden - Set to true if the form should be hidden by default
*
*/
if (isset($hidden)) {
($hidden) ? $hidden = 'display:none' : $hidden = '';
} else {
$hidden = '';
}
if (isset($id)){
$id_string = ".$id.";
}else{
$id_string = ".";
}
/* String Constants */
$input_eduschool_default_text = __("School");
$input_edudegree_default_text = __("Degree");
$input_edufield_default_text = __("Field/s of Education");
$input_edugrade_default_text = __("Grade (What qualification/s did you obtain?");
$input_edudescription_default_text = __("Description");
$input_edustartdate_default_text = __("Start Date");
$input_eduenddate_default_text = __("End Date");
$submit_button_text = __("Save");
?>
<?php echo $this->Form->create('Education', array('controller' => 'Education', 'action' => 'add', 'class' => 'main', 'style' => '$hidden')); ?>
<?php
echo $this->Form->input('Education'.$id_string.'school', array(
'class' => 'white large',
'placeholder' => $input_eduschool_default_text,
'id' => 'edu-school',
'label' => false,
'div' => false,
'type' => 'text'
));
?>
<?php
echo $this->Form->input('Education'.$id_string.'education', array(
'class' => 'white large',
'placeholder' => $input_edudegree_default_text,
'id' => 'edu-education',
'label' => false,
'div' => false,
'type' => 'text'
));
echo $this->Form->input('Education'.$id_string.'study_field', array(
'class' => 'white large',
'placeholder' => $input_edufield_default_text,
'id' => 'edu-field',
'label' => false,
'div' => false,
'type' => 'text'
));
echo $this->Form->input('Education'.$id_string.'qualification', array(
'class' => 'white large',
'placeholder' => $input_edugrade_default_text,
'id' => 'edu-grade',
'label' => false,
'div' => false,
'type' => 'text'
));
echo $this->Form->input('Education'.$id_string.'start_date', array(
'class' => 'white one-third',
'placeholder' => $input_edustartdate_default_text,
'id' => 'edu-start-date',
'label' => false,
'div' => false,
'type' => 'text'
));
echo $this->Form->input('Education'.$id_string.'end_date', array(
'class' => 'white one-third',
'placeholder' => $input_eduenddate_default_text,
'id' => 'edu-end-date',
'label' => false,
'div' => false,
'type' => 'text'
));
echo $this->Form->input('Education'.$id_string.'description', array(
'class' => 'white large',
'placeholder' => $input_edudescription_default_text,
'id' => 'edu-notes',
'label' => false,
'div' => false,
'type' => 'textarea',
'rows' => '5'
));
?>
<?php
echo $this->Form->submit($submit_button_text, array(
'class' => 'intro_submit',
'type' => 'submit'
));
echo $this->Form->end();
?>
请注意,这是一个草稿......我仍然需要实施处理表单数据的操作,并在&#39; education_edit_form&#39;中进行一些额外的验证。元件。但是现在我只想知道如何使用正在编辑的条目中的值来填充编辑表单。
提前谢谢
答案 0 :(得分:0)
我不确定我是否做得对,但我认为你没有关注Cake field naming conventions。
如果您想单独保存每个教育记录(即作为单独的表单处理),您应该简单地使用例如Education.school
作为字段名称。
如果您想同时保存所有教育记录(即将它们作为一个大表格处理),您应该使用Education.0.school
,Education.1.school
名称0
&amp; 1
只是个别记录的索引/计数器(即不是主键值)。
每个教育记录的ID应添加到表格中,如下所示:
echo $this->Form->hidden('Education.id'); // separate form per each record
或
echo $this->Form->hidden('Education.' . $i . '.id'); // one form for all records