Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
Philip

Multiple Dropdowns Same Model

Recommended Posts

Hi guys

 

I've had this issue for a while now and I'm completely stuck. I've been reading the book and found the section on uploading multiple files and I'm just wondering how this could be adapted to my situation.

I'm trying to use dropdown menus to insert course takes into the database. Basically a student can choose a 1st and 2nd choice course and on submit, 2 rows are inserted into the CourseChoices table. I can get the two rows inserted to the DB fine, but only if all the columns have been filled. Basically validation is not working at all and I'm at a loss at how to implement it. To get the dropdowns to insert propertly, I had to change their name attributes so that they did not clash and override each other. This is probably where I am going wrong somehow. I posted a question on stack overflow related this, but I was trying to do it with a CForm, which apparently is not possible with multiple instances of the same model.

 

Here is the link to my question on stack overflow.

http://stackoverflow.com/questions/19526599/changing-cform-dropdown-name-attribute-in-yii

 

Here is what I've done so far:

 

In the controller (I'm using Wizard Behavior, so this is in WizardController.php)

$model = array();
$model['c1p1'] = new CourseChoice();
$model['c1p2'] = new CourseChoice();

if (isset($_POST['courses']) && !empty($_POST['courses'])) {
    $c1p1 = $model['c1p1'];
    $c1p2 = $model['c1p2'];

   $c1p1->course = $_POST['courses']['c1p1'];
   $c1p1->student_id = Yii::app()->user->id;
   $c1p1->year = 2014;
   $c1p1->priority = 1;
   $c1p1->status = "STNSU";

   $c1p2->course = $_POST['courses']['c1p2'];
   $c1p2->student_id = Yii::app()->user->id;
   $c1p2->year = 2014;
   $c1p2->priority = 2;
   $c1p2->status = "STNSU";

   foreach ($model as $i=>$ccModel) {
         if ($ccModel->validate()) {
               $ccModel->save();
               // This is Wizard behavior related step
               $event->sender->save($ccModel->attributes);
         }
   }
   // Using Wizard behavior related to handle the step
   $event->handled = true;

and then the view

<div class="form">
    <?php $form = $this->beginWidget('CActiveForm', array(
        'id' => 'user-form',
        'enableAjaxValidation' => false,
    ));
    /* 
     * This should be in the CourseChoice model, but is here for testing
     * To generate the course lists, CoursePeriod contains a foreign key to Course
     * This is because sometimes a course will run one year, but not the following year
     * or perhaps with a dates. Records therefore need to be keep for archived applications
    */
    $data = CHtml::listData(CoursePeriod::model()->with('course0')->findAll("year = 2014"), 'id', 'course0.course_name');
    ?>
    <?php echo $form->errorSummary($model); ?>

    <fieldset>
        <legend>Course 1</legend>
        <div class="row">
            <?php echo $form->labelEx($model['c1p1'], 'c1p1'); ?>
            <?php echo $form->dropDownList($model['c1p1'], 'course', $data,
                array('name' => 'courses[c1p1]', 'prompt' => 'Please Select A Course')); ?>
            <?php echo $form->error($model['c1p1'], 'c1p1'); ?>
        </div>
        <div class="row">
            <?php echo $form->labelEx($model['c1p2'], 'c1p2'); ?>
            <?php echo $form->dropDownList($model['c1p2'], 'course', $data,
                array('name' => 'courses[c1p2]', 'prompt' => 'Please Select A Course')); ?>
            <?php echo $form->error($model['c1p2'], 'c1p2'); ?>
        </div>
    </fieldset>


    <div class="row buttons" id="buttons">
        <?php
        echo CHtml::submitButton('Previous', array('name' => 'previous'));
        echo CHtml::submitButton('Next', array('name' => 'next'));
        echo CHtml::endForm();
        ?>
    </div>
    <?php $this->endWidget(); ?>
</div><!-- form -->

I would then have to validate this in the CourseChoice model i.e., how I put a rules in when I have changed the name attribute of the dropdowns. For example, Priority 1 is required, but Priority 2 is not.

 

Any ideas where I am going wrong?

Share this post


Link to post
Share on other sites

What you are trying to validate is tabular data, yii does not provide a way to validate this kind of data and therefore it will require you to write your own validation code to loop through each record of the tabular data you need to validate and save.

 

This thread helped me to fix my tabular data problem and get the validation working.

 

http://www.yiiframework.com/doc/guide/1.1/en/form.table

  • Upvote 1

Share this post


Link to post
Share on other sites

Hi Edward

 

Thank you for replying.

I've come across that page while looking into this, but couldn't quite get it working.

I'll give it another read and try again for safe measure.

 

Thanks again

 

What you are trying to validate is tabular data, yii does not provide a way to validate this kind of data and therefore it will require you to write your own validation code to loop through each record of the tabular data you need to validate and save.

 

This thread helped me to fix my tabular data problem and get the validation working.

 

http://www.yiiframework.com/doc/guide/1.1/en/form.table

  • Upvote 1

Share this post


Link to post
Share on other sites

Thanks Edward,

 

It seems I must have made a mistake the first time around, as I got it working through using the tabular input tutorial. 

Basically my view looks something like this now:

...
<?php foreach ($models as $i => $ccModel): ?>
            <div class="row">
                <?php switch ($i) {

                    case "c1p1":
                        echo $form->labelEx($models[$i], $i);
                        echo CHtml::activeDropDownList($models[$i], 'course', $data,
                            array('name' => "CourseChoice[$i]", 'prompt' => 'Select A Course'));
                        echo $form->error($models[$i], $i);
                        break;
                    case "c1p2":
                        echo $form->labelEx($models[$i], $i);
                        echo CHtml::activeDropDownList($models[$i], 'course', $data,
                            array('name' => "CourseChoice[$i]", 'prompt' => 'Select A Course'));
                        echo $form->error($models['c1p2'], $i);
                        break;

                } ?>
            </div>
        <?php endforeach; ?>
...

This works, though I managed to convince management that this is a horrible way to present the data, so I'm off the hook with it anyway! ;-)

Thanks again

  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...