1
votes

I have a scenario in which I have two models, profile and education, with a Profile hasMany Education relationship.

Then I have a view where I want to have multiple forms:

  • One edit form for each Education entry of the user's profile.
  • One form to add a new entry.

The problem here is that I want the edit forms to be filled with the data of each entry. Any ideas on how I can implement this?

CakePHP documentation states that:

The FormHelper uses the $this->request->data property to automatically detect whether to create an add or edit form. If $this->request->data contains an array element named after the form’s model, and that array contains a non-empty value of the model’s primary key, then the FormHelper will create an edit form for that record

I already implemented successfully an edit form but in a case where I only had one form.

Right now I have something like this:

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;

}

/* (...) */

}

As for the view file...

 <?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));
    ?>

The 'education_edit_form' element:

    <?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();
?>

Note please that this is a draft... I still have to implement the actions to process the form data and do some additional validations in the 'education_edit_form' element. But right now I only want to know how to fill the edit forms with the values from the entry being edited.

Thank you in advance

1

1 Answers

0
votes

I'm not sure if I get this right but I think you are not following Cake field naming conventions.

If you want to save each education record separately (i.e. process as a separate form) you should simply use e.g. Education.school as a field name.

If you want to save all education records at the same (i.e. process them as one big form) you should use Education.0.school, Education.1.school names where 0 & 1 are simply an index/counter of individual records (i.e. not primary key values).

The id of each Education record shall be added to the form as follows:

echo $this->Form->hidden('Education.id'); // separate form per each record

or

echo $this->Form->hidden('Education.' . $i . '.id'); // one form for all records