4

I want to disable certain textfields and dropdown lists to prevent user from changing its values. But whenever I try to, it doesn't collect/get the data of that specific disabled textfield or dropdown list.

Here's my view where I display my dropdown lists. It's inside a for loop:

echo $form->field($model1[$i], 'earning_item_id')->widget(Select2::classname(), [
    'data' => $earningslistData,
    'options' => ['placeholder' => '', 'prevOptionID' => $model1[$i]->earning_item_id, 'prevOptionName' => $earningslistData[$model1[$i]->earning_item_id], 
                    "name" => "EarningDetails[".$i."][earning_item_id]", "row_count1" => $i],
    //'disabled' => true,
    'pluginOptions' => [
        'allowClear' => true,
        'label' => false
    ]
]);

Here's how it looks like without disabling them:

enter image description here

Then, when I save it, it looks like this:

enter image description here

But, when I disable the dropdown lists, it will give me this:

enter image description here

I think the Full Name comes from my model but I don't know why:

public function getFullName() 
{
    return $this->user ? $this->user->fname . ' ' . $this->user->lname : 'Full Name';
}

It goes the same when I disable a textfield:

echo $form->field($model, 'user_id')->widget(Select2::classname(), [
    'data' => $listData,
    'options' => ['placeholder' => 'Select a Staff'],
    'disabled' => true,
    'pluginOptions' => [
        'allowClear' => true,
    ],
])->label('Employee Name');

I am using Kartik widgets for my form fields. Is there a way to fix this? Please tell me how.

EDIT

Thanks to the commenters below I found out the difference between disabled and readonly. Since it's a dropdown list, here's what I did:

echo $form->field($model, 'user_id')->widget(Select2::classname(), [
    'data' => $listData,
    'options' => ['placeholder' => 'Select a Staff', ],
    'pluginOptions' => [
        'allowClear' => true,
    ],
])->label('Employee Name');

echo $form->field($model, 'user_id')->widget(Select2::classname(), [
    'data' => $listData,
    'options' => ['placeholder' => 'Select a Staff', 'style' => 'display:none'],
    'pluginOptions' => [
        'allowClear' => true,
    ],
])->label('');
kaynewilder
  • 853
  • 6
  • 28
  • 53
  • 3
    Instead of disabled property try readOnly = true – Chinmay Waghmare Apr 30 '15 at 04:10
  • 2
    You should use readonly, disable field will not send data to server. You can check this answer for more information http://stackoverflow.com/a/7730719/1635676 – Touqeer Shafi Apr 30 '15 at 04:46
  • thanks for your response. adding a `readonly` in this widget would be `'readonly' => true,` but it doesn't seem to work. it is still editable. seems like nothing changed. – kaynewilder Apr 30 '15 at 05:38
  • I think because it's widget (not standard field), it should have speical option for that. – arogachev Apr 30 '15 at 06:21
  • @arogachev in Kartik's widget, it should be `'readonly' => true,` based in his documentation. but it doesn't seem to work. I found this: http://stackoverflow.com/questions/8100351/how-to-make-a-dropdown-readonly-using-jquery and someone said that "*drop down is always read only . what you can do is make it disabled*" – kaynewilder Apr 30 '15 at 06:24

1 Answers1

9

Disabled html form field will not submit, the problem is not with yii itself. The solution in this case is to have 2 copies of the same field, one as disabled as you have already included and the other one hidden with the same value as below after the original one.

echo $form->field($model1[$i], 'earning_item_id')->hiddenInput()->label('');
arkoak
  • 2,437
  • 21
  • 35
  • I was gonna answer my own question with the same thought as you did. thanks! here's my reference: http://stackoverflow.com/questions/368813/html-form-readonly-select-tag-input – kaynewilder Apr 30 '15 at 08:58