How to create checkboxlist from model

I’m trying to create a checkboxlist that will get value from database
my code is

public function run()
        if (Yii::$app->user->isGuest) {

        $notification = NotificationType::find()->all();
        $notificationtype = new NotificationType();

        return $this->render('NotificationCheckboxWidget', [
            'notification' => $notification,
            'notificationtype' => $notificationtype,

View :

<?= Html::activeCheckboxList($notification, 'dropbox', ArrayHelper::map($notificationtype, 'text','type')) ?>`

Model :

public function rules()
        return [
            [['dropbox'], 'integer'],
            [['type'], 'string', 'max' => 10],
            [['text'], 'string', 'max' => 300],

it’s return empty div

Hi @bynd,

activeCheckboxList($model, $attribute, $items, $options = [])
  • $model … The model that is related with the form. It is usually a “Form-Only” model, not an ActiveRecord model backed by a db table.
  • $attribute … The attribute that will get the selected result. It must be an array, because the checkbox list handles an array.
  • $items … The checkbox items. It must be an array of '$value => $label' pairs. $label is used to render the checkbox label, and $value will come into the array of $attribute when it is selected.
1 Like

Check the following thread for a sample code.

You have an errors in passed params to the map() function.

At first, swap $notificationtype and $notification. It should looks like:
Html::activeCheckboxList($notificationtype, 'dropbox', ArrayHelper::map($notification, 'text','type')).

Use pluralized names for arrays or any other collections. not $notification, but $notifications

The second error is pointing incorrect key in map() function(it is the second parameter). It MUST be a column with type an integer.
E.g, let’s assume, in NotificationType model you have attribute like id(it is your primary key in the related table), so id must be passed as the second parameter, and third parameter may be type or text.
something like this:
<?= Html::activeCheckboxList($notificationtype, 'dropbox', ArrayHelper::map($notification, 'id','type')) ?>