yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.91k forks source link

Yii2 Checkbox value not rendering properly from array #11843

Open dev-mraj opened 8 years ago

dev-mraj commented 8 years ago

What steps will reproduce the problem?

Lets talk in code directly.

<div class="checkbox-list">
                    <?php
                    var_dump ($model->categoriesIds); // [1,2] so database has two categories.
                   // controller saving them properly..
                    $cats=Category::find()->all(); foreach($cats as $i=>$category){?>
                        <?= $form
                            ->field($model, 'categoriesIds[]')
                            ->checkbox([
                                'label'=>$category->name,
                                'value' => $category->id
                            ])
                            ->label(false)
                        ?>
                    <?php } if(count ($cats)==0){ echo '<li>No Categories found.</li>';} ?>
                </div>

i've categories's values but it don't check checkboxes based on this array, its saving values as well properly.

What is the expected result?

When database has (categoriesIds) has array like [1,2] then checkbox state sholud be checked

(i think as previously in yii1 it was working but not sure)

What do you get instead?

its not checked at all

Additional info

BaseHtml::getAttributeValue

Solution

<div class="checkbox-list">
                <?php
                var_dump ($model->categoriesIds); // [1,2] so database has two categries.
                $cats=Category::find()->all(); foreach($cats as $i=>$category){?>
                    <?= $form
                        ->field($model, in_array ($category->id,$model->categoriesIds)?'categoriesIds['.array_search($category->id,$model->categoriesIds).']':'categoriesIds[]')
                        ->checkbox([
                            'label'=>$category->name,
                            'value' => $category->id
                        ])
                        ->label(false)
                    ?>
                <?php } if(count ($cats)==0){ echo '<li>No Categories found.</li>';} ?>
            </div>
Q A
Yii version f5fe6ba58dbc92b37daed5d9bd94cda777852ee4
PHP version 5.5
Operating system windows
pstddc commented 8 years ago

open checkboxList

pstddc commented 8 years ago

guide say:

// поддерживает выбор нескольких значений:
echo $form->field($model, 'items[]')->checkboxList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']);

you read guide?

dev-mraj commented 8 years ago

i've already tried this thing.

<? 
    use yii\helpers\ArrayHelper;
    $cats=Category::find()->all();
    echo $form->field($model, 'categoriesIds[]')->checkboxList(ArrayHelper::map($cats, 'id', 'name'));
    ?>
samdark commented 8 years ago

Unable to reproduce it. @dev-mraj would be great if you'll provide a clear way reproducing the issue.

desaikalpesh34 commented 5 years ago

@samdark

Don't use checkboxList. use checkbox with loop its reproducible bug.

alexkart commented 5 years ago

It seems that it had already been fixed in https://github.com/yiisoft/yii2/issues/11973 but then reverted because of BC break. So, this example should be removed from the documentation:

// allow multiple items to be checked:
echo $form->field($model, 'items[]')->checkboxList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']);