1

Currently, I have an existing code to show user status as 'Inactive' or 'Active' based on ban_time field of table User. When status of User is 'Inactive', the ban_time field will be updated by current timespan (I guess it use external wrapper plugin)

$form->field($user, 'ban_time')->widget(SwitchInput::classname(), [
            'type' => SwitchInput::CHECKBOX,
            'containerOptions' => ['class' => 'inner-form-group'],
            'pluginOptions' => [
                'state' => empty($user->ban_time),
                'handleWidth' => 60,
                'onText' => 'Active',
                'offText' => 'Inactive'
            ],
            'pluginEvents' => [
                "switchChange.bootstrapSwitch" => "function(event, state) { $('[name=\'User[ban_time]\']').val(state ? 0 : 1) }",
            ]
        ])->label('Status'); 

Now, I need to add more status instead of 'Inactive' or 'Active'. So I want to change this field into dropDownList but when changing status of User, ban_time was not changed

$form->field($user, 'ban_time')->dropDownList(
            [empty($user->ban_time) =>'Active', !empty($user->ban_time) =>'Inactive']
        )->label('Status'); 

Please help me how to change it

Quan Nguyen
  • 559
  • 4
  • 23
  • 47

2 Answers2

1

You can make your form element as dropDownList as

     $items = [1 =>'Active', 0 =>'Inactive' ,2 => 'Subscribed' ,3 => 'Deleted'];
     $form->field($user, 'ban_time')->dropDownList($items)->label('Status'); 

See DropDownList

Double H
  • 4,120
  • 1
  • 15
  • 26
  • As user wrote, the actual problem is saving `ban_time`, not displaying drop-down list. Also this is not good example, values should be constants in model, and list of value-label pairs retrieved through separate static method. View should not expose this. Also it violates DRY principle. – arogachev Nov 17 '15 at 11:24
  • But how can I update value and set default value for it? – Quan Nguyen Nov 17 '15 at 11:24
  • @KenNguyen Update your question with more info, because it's unclear in current state. – arogachev Nov 17 '15 at 11:25
1

for example if you have dropdownlist like this given below

  echo $form->dropDownListGroup(
                    $model, 'status', array(
                'wrapperHtmlOptions' => array(),
                'widgetOptions' => array(
                    'data' =>$model->getDropdownvalue(),
                    'htmlOptions' => array(
                        'prompt' => 'Select Project',
                        'ajax' => array(
                                'type' => 'POST',
                                'url' => your url,
                                //'dataType' => 'json',
                                'data'=>array('status'=>'js:this.value'),

                              )

in your controller you will get the value of dropdown list using url

public function actiondropdownvalue(){
$model = new status();
        $status = $_POST['status'];
       $model->save();

this example is only showing how it will work . You will need user id to save status for particular user to update or save status.

arogachev
  • 33,150
  • 7
  • 114
  • 117
Amitesh Kumar
  • 3,051
  • 1
  • 26
  • 42