0
votes

I have a Product model with a 'categories' column. This column should be able to contain data from a Checkboxlist. Whenever a user creates a new Product I want the form to display the 'categories' Checkbox, with the items set as active from the user's last created Product.

So for instance: the 'categories' Checkboxlist contains items 'Movies' and 'Music'. When the user checks 'Movies' and creates the Product, the next time the user goes to create a Product, 'Movies' is already selected (because it saves the selection from the user's previous product creation).

I believe these are the most effective steps to code in order to achieve this goal:

When Product is created: checked items are saved to 'categories' column in Product Model & a 'categories' column in the User's 'Profile' model

User's last saved categories ('categories' column in Profile model) should be retrieved at the Product's Create form. Code in Product model's _form view:

<?php echo $form->checkBoxList($model, 'categories', array('movies'=>'Movies','music'=>'Music')); ?>

(I'm unsure as to where to set an array for active values)

I'll need to convert the Array of the selected Checkboxes to a string using explode(",", $model->categories) so I can put it inside the 'categories' columns of the 'Product' and 'Profile' models by using the ProductController's actionCreate function.

Then to set the user's last selected Checkboxlist items as active in the Product _form view I need to convert the $model->categories data back to an array by imploding the column ie implode(",", $user->profile->categories).

How would you code this in Yii?

1

1 Answers

1
votes

Use CHtml::ckeckboxList instead of activecheckbox. It have selected parameter. Selected there is array of key=>value pairs. yiiframework.com/doc/api/1.1/CHtml#checkBoxList-detail

Or you can rewrite (extend) you CHtml helper to resolve your format.

For example:

public static function activeCheckBoxList($model,$attribute,$data,$htmlOptions=array()) {

        self::resolveNameID($model,$attribute,$htmlOptions);
    if(array_key_exists('explode_format',$htmlOptions))
    {
        $selection=explode($model->$attribute);
        unset($htmlOptions['explode_format']);
    }
        else{
        $selection=self::resolveValue($model,$attribute);
        }
    if($model->hasErrors($attribute))
        self::addErrorCss($htmlOptions);
    $name=$htmlOptions['name'];
    unset($htmlOptions['name']);

    if(array_key_exists('uncheckValue',$htmlOptions))
    {
        $uncheck=$htmlOptions['uncheckValue'];
        unset($htmlOptions['uncheckValue']);
    }
    else
        $uncheck='';

    $hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false);
    $hidden=$uncheck!==null ? self::hiddenField($name,$uncheck,$hiddenOptions) : '';

    return $hidden . self::checkBoxList($name,$selection,$data,$htmlOptions);
} 

Then just add 'explode_format'=>true to your htmloptions for activecheckboxlist. Something like this will work.