yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

CheckboxColumn submitted value is inconsistent with the value submitted by checkboxList

Open PowerGamer1 opened this issue 9 years ago • 12 comments

When you have an ActiveForm with checkboxList() and none of the checkboxes is selected by a user, the form submits an empty array.

When you have a GridView with CheckboxColumns inside an ActiveForm and none of the checkboxes is selected by a user, the form does NOT submit an empty array for checkboxes.

In second case an empty array should be submitted too. Consider the following example: initially some checkboxes in CheckboxColumn are selected by default and the form is submitted by GET. The server will not be able to distinguish between the first opening of the page or the submitted form when the user have unselected ALL checkboxes.

PowerGamer1 avatar Jul 23 '15 08:07 PowerGamer1

@PowerGamer1 , what's the status of this issue. Have you found a solution for it already?

dynasource avatar Dec 03 '16 12:12 dynasource

@dynasource I didn't try to fix this issue in the engine. And I don't remember what workaround I used in my code (may be I decided not to use CheckboxColumn in my code at all).

PowerGamer1 avatar Dec 03 '16 12:12 PowerGamer1

In order for our issue tracker to be effective, we are closing this issue.

If you want it to be reopened again, feel free to supply us with the requested information.

Thanks!

This is an automated comment, triggered by adding the label expired.

yii-bot avatar Dec 05 '16 20:12 yii-bot

Closing as expired one

SilverFire avatar Dec 05 '16 20:12 SilverFire

The problem I reported is still unfixed and present in latest version of Yii2. Why this issue has been closed and what does "expired" even mean? Are you no longer fixing issues in Yii2 or what?

PowerGamer1 avatar Dec 05 '16 20:12 PowerGamer1

@PowerGamer1 I think this was a misunderstanding. It is not really clear for us whether there is an issue that needs to be fixed here. If you have worked around it and noone else needs this behavior to be changed we close the issue and focus on more important things. If you think this issue is important and something should be changed, we can reopen it.

cebe avatar Dec 05 '16 21:12 cebe

agreed

dynasource avatar Dec 05 '16 22:12 dynasource

I think this issue is important enough not to be closed. It is ok if you give it a low priority though.

Actually, the issue affects not only CheckboxColumn but also checkbox list generated with Html::checkboxList() helper.

<?php $form = ActiveForm::begin(); ?>
<?= $form->field($someModel, 'someAttr')->checkboxList([1=>'item1', 2=>'item2']) ?>
<?= Html::checkboxList('list2', null, [3=>'item3',4=>'item4']) ?>
<?= GridView::widget([
	'dataProvider' => $dataProvider,
	'columns' => [
		[
                    'class' => \yii\grid\CheckboxColumn::class,
                   'name' => 'list3',
		],
               // ...
	],
]) ?>
<?= Html::submitButton('Create') ?>
<?php ActiveForm::end(); ?>
<?php var_dump($_POST) ?>

In the example above if you do not check any checkboxes and submit the form the $_POST array will have someAttr value of checkbox list associated with a model but will not have a value for list2 and list3 of checkbox list created through other means (Html::checkboxList() and CheckboxColumn). At the very least this is an inconsistent behavior.

PowerGamer1 avatar Dec 06 '16 08:12 PowerGamer1

thanks for taking ownership on this issue

dynasource avatar Dec 06 '16 09:12 dynasource

Just for the record: nowhere have I said that I am "taking ownership on this issue" or plan to implement a fix. If lack of information or understanding prevents you from "verifying" the issue ask me questions. If everything is clear to you and for some reason you decide not to implement the changes/fix explain that reason here. If you don't have time to fix it now (or think this issue is insignificant but still an issue) leave it open so that you or someone else can fix it later.

This is what in my opinion needs to be done with regard to this issue:

  1. Html::chechboxList() allows setting $options['unselect'] to generate hidden input. When called directly by default $options['unselect'] is not set and no hidden input is generated. When called through Html::activeCheckboxList() by default $options['unselect'] will be set and hidden input will be generated. This is inconsistent and needs to be: either in both cases no hidden input by default or in both cases there is a hidden input by default.

  2. CheckboxColumn needs an option analogous to $options['unselect'] in Html::chechboxList() to allow generation of hidden input. This option should be either on or off by default depending on the resolution of p1.

If you disagree on any of the points above I'd like to here arguments why.

PowerGamer1 avatar Dec 06 '16 12:12 PowerGamer1

@PowerGamer1, take it easy. Your effort to have this issue resolved shows 'ownership' of the issue.

dynasource avatar Dec 06 '16 13:12 dynasource

As a temporary solution it can be handled by overriding a Model::load()

    public function load($data, $formName = null)
    {
        $scope = $formName ?? $this->formName();
        if ($data && !isset($data[$scope]['checkboxColumnInputName'])) {
            $data[$scope]['checkboxColumnInputName'] = [];
        }
        return parent::load($data, $formName);
    }

matperez avatar Feb 08 '21 13:02 matperez