Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 Doesn't Get the Value of Disabled Dropdown List or Textfield

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('');
like image 578
kaynewilder Avatar asked Jan 08 '23 11:01

kaynewilder


1 Answers

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('');
like image 172
arkoak Avatar answered Jan 11 '23 02:01

arkoak