Yii Framework Forum: dropdownlist selected value - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

dropdownlist selected value

#1 User is offline   folumike 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 158
  • Joined: 27-November 14

Posted 13 March 2018 - 08:07 AM

How do I write the code to save into my database based on the value in my dropdownlist when the save button is clicked.The dropdownlist value can either be equal to 'weekly format' or 'Topic format'
The attribute name for the dropdownlist is 'course_format'
for example,
If dropdownlist = 'weekly format'
I want to do this in the controller.


class Course extends \yii\db\ActiveRecord
    const TYPE_WEEKLY= 'Weekly format';
    const TYPE_TOPIC='Topic format';    

    public static function tableName()
        return 'course';
    public static function getCourseFormat()
    Yii::t('app', self::TYPE_TOPIC) => Yii::t('app', 'Topic format'),
    Yii::t('app', self::TYPE_WEEKLY) => Yii::t('app', 'Weekly format'),



        <div class="col-xs-12 col-sm-4 col-lg-4">
          <?= $form->field($model, 'course_format')->dropDownList($model->getCourseFormat(),['prompt'=>'Select Course Format']) ?>

      <div class="col-xs-6">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-block btn-success' : 'btn btn-primary']) ?>


public function actionCreate()
    $request = Yii::$app->request;
    $model = new Course();
    // if post data was submitted, try to load it into the new model
    if ($request->isPost && $model->load($request->post())) {
        // check if topic format 
        if($model->course_format == Course::TYPE_TOPIC){
            // do your topic related stuff 
        // check if weekly format 
        if($model->course_format == Course::TYPE_WEEKLY){
            // do your weekly related stuff 
        // don't forget to save your model 
            // redirect user to view 
            return $this->render('view', ['model' => $model->id]);
    // will be executed by default 
    // when no post data is submitted or model->save() failed
    return $this->render('create', [
        'model' => $model,


If course_format is weekly and course_format_no is 3, the start_date (Datepicker) will be selected. If start_date is 18th November, 2016. then lecture_name will be something like this:
18 November - 24 November,
25 November - 1 December,
2 December - 8 December.
And lecture_code will be something like this:
Note that lecture_name is increased by one week.
lecture_name and lecture_code are columns in lectures table.
It should be saved in different fields and not as a big string

Where I have issue is where course_format is weekly.
Note that is a date picker.

Attached File  course format.PNG (3.42K)
Number of downloads: 8

Attached File  course start date.PNG (1.29K)
Number of downloads: 8

Share this topic:

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users