0

I have a cakephp 1.3 app which get's a person's consent to participate in a study. I'm trying to link inclusion criteria and exclusion criteria for the study on the Add study page. The HABTM relationship is setup and working(If there's only one way to select them) What I'm trying to do now is display the current Inclusion Criteria as a list of checkboxes the person adding the study can select, but I also want a field where new criteria can be entered as a comma seperated list. Right now it will save the checkboxes, but I want it to also add the text entered in the field as well. Screen showing the Add study as it sits now is below. I don't really know how I would go about doing that. Is there a way to do this or will I have to take the data entered, sort through it and then add it to $this->data[inclusion field data]? An example would be nice. :) I've added the code I have so far below the image. Add Study Screen

Controller:

/**
   * 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,)));
  }

View:

<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>
4

1 回答 1

1

You are trying to do pretty much the same as he wanted to do: Saving tags into a database table in CakePHP

What you have to do is:

  1. Check if the manual inclusion field is empty or not. If the field is empty ignore it and go to step 5
  2. If the field is not empty, split it at comma or whatever sign you want to use as a seperator. You can now validate it if you want to.
  3. Use all the inclusions of step 3 and save them in your custom or real inclusion table. You should keep track of those custom entries in some way, so you don't mess them up with those you provided. Save all the ids generated while saving within an array.
  4. Merge the collected ids of step 3 with those you got from the form which were given by you.
  5. Save all collected and given data to database

The answere in the post given above including the code there can be used to do that. You should get along with it pretty well if you read the whole post + code comments ;)

If you have any further questions, just ask ;)

Greetings func0der

于 2012-11-15T21:19:41.880 回答