我有一个cakephp 1.3应用程序,它获得一个人的同意参与研究。我试图在添加研究页面上将研究的纳入标准和排除标准关联起来。 HABTM关系设置和工作(如果只有一种方法可以选择)我现在要做的是将当前的包含条件显示为添加研究的人员可以选择的复选框列表,但是我也想要一个字段新标准可以作为逗号分隔列表输入。现在它将保存复选框,但我希望它也添加在该字段中输入的文本。屏幕上显示添加研究现在位于下方。我真的不知道我会怎么做。有没有办法做到这一点,或者我将不得不采取输入的数据,整理它,然后将其添加到$ this-> data [包含字段数据]?一个例子会很好。 :)我已经添加了图片下面的代码。 Cakephp 1.3从列表中选择字段选项或输入新的
控制器:
/**
* Add a study.
*/
function add() {
if (!empty($this->data)) {
//get the inclusions from the text data
//Sort through the comma seperated list and add it to $this->data['Study']['Inclusions']???
$this->Study->create();
if ($this->Study->save($this->data)) {
$this->Session->setFlash(__('The study has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The study could not be saved. Please, try again.', true));
}
}
$this->set('inclusions', $this->Study->Inclusion->find('list', array(
'fields' => array('id', 'name'),
'order' => 'Inclusion.name',
'recursive' => 0,
)));
$this->set('exclusions', $this->Study->Exclusion->find('list', array(
'fields' => array('id', 'name'),
'order' => 'Exclusion.name',
'recursive' => 0,
)));
$this->set('forms', $this->Study->Form->find('list', array('order' => 'Form.name','recursive' => 0,)));
}
查看:
<div class="studies form">
<?php echo $this->Form->create('Study', array('type' => 'file'));?>
<fieldset>
<legend><?php __('Add Study'); ?></legend>
<?php
echo $this->Form->input('studyname', array('label'=>'Study Name','required' => true));
echo $this->Form->input('studynumber', array('label'=>'Study Number','required' => true));
echo $this->Form->input('file', array('label'=>'Select Electronic Consent','type' => 'file'));
echo $this->Form->input('consent_form_date');
?>
<fieldset class="inclusion">
<legend><?php __('Inclusions'); ?></legend>
<div class="IncExc">
<?php
echo $this->Form->input('Inclusion',array(
'label' => false,
'type' => 'select',
'multiple' => 'checkbox',
'options' => $inclusions,
'selected' => $html->value('Inclusion.Inclusion'),
));
?>
</div>
<?php
echo $form->input('Inclusion.inclusions',array(
'type' => 'text',
'label' => __('Add New Inclusions',true),
'after' => __('Seperate each new Inclusion with a comma. Eg: family, sports, icecream',true)
));
?>
</fieldset>
<fieldset>
<legend><?php __('Exclusions'); ?></legend>
<div class="IncExc">
<?php
echo $this->Form->input('Exclusion',array(
'label' => false,
'type' => 'select',
'multiple' => 'checkbox',
'options' => $exclusions,
'selected' => $html->value('Exclusion.Exclusion'),
));
?>
</div>
</fieldset>
<fieldset style="width: 700px;">
<legend><?php //__('Forms'); ?></legend>
<?php /*
echo $this->Form->input('Form',array(
'label' => false,
'type' => 'select',
'multiple' => 'checkbox',
'options' => $forms,
'selected' => $html->value('Form.Form'),
));
*/ ?>
</fieldset>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
所以你想将逗号分隔列表转换为HABTM项目?如果是这样,就你的解析它的评论,并将其存储在数组中,然后保存,你就在正确的轨道上。没有“简单”的方式。 – Dave
@Dave是的,我试图做到这一点,同时保持用户选择的HABTM项目。所以存储的是用户选择的项目+用户输入的项目。你能举一个例子说明这个语法是什么吗?将排序后的列表添加到选定列表中?我可以解析它,我只是不知道如何将它添加到已经存在的/被选中的。 – Jonathan
只是从提交中调试()你在Controller中收到的数据,并与之匹配。 Bahaha, – Dave