Changing CForm dropdown name attribute in Yii

636 Views Asked by At

I'm using Yii and I'm having a little problem with some dropdowns. Basically I'm using CForm to display some dropdown menus of courses. A student can select up to two courses and for each course choice the student can select a 1st choice and second. It is a requirement that each course choice is inserted separately into the database. For example, it a student wants to study 2 courses and wants to have a 1st and 2nd priority course, they would choose like this:

  1. Course one - 1st Priority
  2. Course one - 2nd Priority
  3. Course two - 1st Priority
  4. Course two - 2nd Priority

This would put 4 new rows into the database. The administrators of the courses want this displayed as 4 dropdown menus containing the courses.

At the moment, I'm testing with just the 1st and 2nd priorities of course one, but the problem is that course one - priority one is always empty unless a value is selected for priority two. Then priority one gets the same value as priority two, even though two different courses are selected. I've been following this tutorial Form Builder as I am using the Wizard Behavior which uses CForm to build the forms.

Here is my code so far, again only dealing with "course one":

This is a snippet of the relevant code from the controller:

// inside controller
$model = new CourseChoice();
$form = new CForm('application.views.wizard.ccForm', $model);
$form['courseOneP1']->model = new CourseChoice();
$form['courseOneP2']->model = new CourseChoice();

$c1p1 = $form['courseOneP1']->model;
$c1p2 = $form['courseOneP2']->model;

// Here I am just reading the attributes and exiting for testing
if ($form->submitted()&& $form->validate()) {
      echo '<pre>';
      print_r($c1p1->attributes);
      print_r($c1p2->attributes);
      echo '</pre>';
      exit;
      ..........

And here is code in the form in ccForm

return array(
   'showErrorSummary' => true,
   'title' => 'Course Choice 1',
   'elements' => array(
       // Course 1 - 1st Priority
       'courseOneP1' => array(
           'type' => 'form',
           'elements' => array(
               'course' => array(
                   'label' => '1st Priority',
                   'type' => 'dropdownlist',
                   'id' => 'c1p1',
                   'prompt' => 'Select 1st Priority Course',
                   'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
               )
           ),
       ),
       // Course 1 - 2nd Priority
       'courseOneP2' => array(
           'type' => 'form',
           'elements' => array(
               'course' => array(
                   'label' => '2nd Priority',
                   'type' => 'dropdownlist',
                   'id' => 'c1p2',
                   'prompt' => 'Select 2nd Priority Course',
                   'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
               )
           ),
       ),
   ),
   'buttons' => array(
       'previous' => array(
           'type' => 'submit',
           'label' => 'Previous'
       ),
       'submit' => array(
           'type' => 'submit',
           'label' => 'Next'
       )
   )
  );

So lets say I choose 2 courses, one with an id of 15 and the other with an id of 86, I get the following when I print_r() both dropdowns:

Array // Dropdown 1
   (
      [course] => 86
      .... // other irrelevant attributes
   )
Array // Dropdown 2
   (
     [course] => 86
     .... // other irrelevant attributes
   )

Update

I've been looking further into this and when I look at firebug, I see that both dropdowns have the same name:

<div class="row field_course">
  <label for="c1p1">1st Priority</label>
  <select id="c1p1" name="CourseChoice[course]">
</div>
<div class="row field_course">
  <label for="c1p2">2nd Priority</label>
  <select id="c1p2" name="CourseChoice[course]">
</div>

So the 2nd menu is overwriting the first. But how can I change this? If I change 'course'=>array(.... in the CForm for either subform, the applicable dropdown does not render. I have already tried adding 'name'=>'course1' in the form but it makes no difference.

2

There are 2 best solutions below

0
On

Just to answer my own question and close it as it is pretty old now, CForm does not support tabular input and would need to extended to achieve this. Probably not a big job but in the end I convinced management that the four dropdowns design was horrible. :-) I went with a more flexible design showing a gridview of courses in a pop-up to choose courses instead which works well and is less confusing for the user.

Anyone interested in this issue can see the open issue here. There is a link in there to view a possible implementation of extending CForm, though this was posted at the end of 2009.

1
On

Couldn't you just set the name of the 2nd priority input element?

'course' => array(
     'label' => '2nd Priority',
     'name' => 'course2',
     'type' => 'dropdownlist',
     'id' => 'c1p2',
     'prompt' => 'Select 2nd Priority Course',
     'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)