kartik-v / yii2-widget-switchinput

A Yii2 wrapper widget for the Bootstrap Switch plugin to use checkboxes & radios as toggle switches (sub repo split from yii2-widgets)
Other
38 stars 11 forks source link

disabled save wrong value #34

Closed DPineault closed 4 years ago

DPineault commented 4 years ago

I'm using the switchinput as follows

    $form->field($model, 'Active',
    [
        'inputOptions' => [
            'value' => Yii::$app->formatter->asInteger($model->Active), 
            'class' => 'form-control',
        ],
        'labelOptions' => ['class' => 'control-label'],
        'horizontalCssClasses' => [
            'class' => '',
            'wrapper' => '',
        ],
        'template' => '<div class="col-sm-2">{label}</div><div class="col-sm-4">{input}{error}</div>',
    ])
    ->widget(SwitchInput::classname(), 
    [
        'options'=>[
            'tabIndex'=>'345',
        ],
        'pluginOptions' => [
            'size' => 'small',
            'onText' => 'Yes',
            'offText' => 'No',
            'onColor' => 'success',
            'offColor' => 'danger',
        ],
        'disabled' => (Yii::$app->user->can('Administrator') ? false : true),
    ])

If the use is Administrator, everything works just fine, but, if not, then when the form loads, it displays the value correctly, but always saves all values as No/0, even if it was displaying Yes/1.

My workaround has been to add javascript before submit to enable all controls.

craiglondon commented 4 years ago

@DPineault If the input is "disabled" then any change to it is not submitted to the form. I would suggest to use readonly instead of disabled.

kartik-v commented 4 years ago

Correct @craiglondon