在Yii中更改CForm下拉列表名称属性

时间:2013-10-22 19:27:08

标签: yii yii-cform

我正在使用Yii而且我有一些下拉菜单问题。 基本上我正在使用CForm来显示课程的一些下拉菜单。学生可以选择最多两门课程,每个课程选择学生可以选择第一选择和第二选择。要求每个课程选择单独插入数据库。例如,学生想要学习2门课程并希望获得第1和第2优先课程,他们会选择这样的:

  1. 课程一 - 第一优先
  2. 课程一 - 第二优先
  3. 课程二 - 第一优先
  4. 课程二 - 第二优先
  5. 这将在数据库中放入4个新行。课程管理员希望将其显示为包含课程的4个下拉菜单。

    目前,我正在测试当然只有第一和第二优先级,但问题是课程一 - 优先级一直是空的,除非为优先级二选择了一个值。然后优先级1获得与优先级2相同的值,即使选择了两个不同的课程。我一直在关注本教程Form Builder,因为我正在使用使用CForm构建表单的向导行为。

    到目前为止,这是我的代码,仅涉及“课程一”:

    这是来自控制器的相关代码的片段:

    // 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;
          ..........
    

    这是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'
           )
       )
      );
    

    所以我想说我选择了两门课程,一门编号为15,另一门编号为86,当我打印__()两个下拉列表时,我得到以下内容:

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

    更新

    我一直在深入研究这个问题,当我看到萤火虫时,我发现两个下拉列表都有相同的名称:

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

    所以第二个菜单会覆盖第一个菜单。但是我怎么能改变这个呢?如果我更改'course'=&gt;数组(在两个子表单的CForm中....,适用的下拉列表不会呈现。我已经尝试在表单中添加'name'=&gt;'course1'但它没有差。

2 个答案:

答案 0 :(得分:0)

你能不能只设置第二优先级输入元素的名称?

'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'),
)

答案 1 :(得分:0)

只是回答我自己的问题并关闭它,因为它现在很老了,CForm不支持​​表格输入,需要扩展才能实现这一点。可能不是一个大工作,但最后我说服管理层,四个下拉设计是可怕的。 :-)我选择了一个更加灵活的设计,在弹出窗口中显示一个网格视图,以便选择适合的课程,而且不会让用户感到困惑。

任何对此问题感兴趣的人都可以看到未解决的问题here.其中有一个链接可以查看扩展CForm的可能实现,尽管这是在2009年底发布的。

相关问题