Generating Checkbox list from db

Hi,
This is my softwares table;

soft_id soft_name
1 Oracle
2 Revel
3 Spa
4 Office
5 Spageo
6 Kingdom

I want to generate a checkbox list which loads all the software names.So for each time a new software is added into the database a checkbox list is created.What are the steps ? Thanks

Hi @sangyogn,

There are many ways to accomplish your needs, because Yii is very flexible. So the following is just a sample.

  1. Create an ActiveRecord model for “software” table. You may make use of Gii’s model generator for it.
  2. Write a static method in the model that lists all the software.
    class Software extends ActiveRecord
    {
        ...
        /**
         * @return array software list
         */
        public static function getSoftwareList()
        {
            $data =  static::find()
                ->select(['soft_id', 'soft_name'])
                ->orderBy('soft_name')->asArray()->all();
            return ArrayHelper::map($data, 'soft_id', 'soft_name');
        }
    
    }
    The method should return an array of softwares. The key should be soft_id and the value should be soft_name. For example:
    [
        [1 => 'Oracle'],
        [2 => 'Revel'],
        [3 => 'Spa'],
        ...
    ];
    
    ArrayHelper::map() will take care of this transformation for you.
  3. Create a model for the input form. Let’s say it’s MyForm. And in the model declare a variable for the software selection. It should be an array of integers.
    class MyForm extends Model
    {
        ...
        /**
         * @var integer[] software selection
         */
        public $soft_ids;
        ...
    }
    
  4. And write a rule for it. You should use each and exist validators.
    public function rules()
    {
        return [
            ...
            ['soft_ids', 'each', 'rule' => [
                'exist', 'tagetClass' => Software::className(),  'targetAttribute' => 'soft_id']
            ],
            ...
        ];
    }
    
    This ensures that each input value of soft_ids is a valid soft_id.
  5. In the form that gathers data using MyForm, you can use ActiveField::checkboxList like the following:
    <?= $form->field($model, 'soft_ids')->checkboxList(Software::getSoftwareList()) ?>
    

Note that you should use Html::checkboxList() or Html::activeCheckboxList() instead of ActiveField::checkboxList() when you don’t want to use ActiveForm. But all of these methods take the same parameter of $items that is an array of checkbox items, keys being the input values and the values being the visible labels. That’s why we write Software::getSoftwareList() like that.

1 Like

Thank you very much.Its helping me a lot

Iam trying to render just the checkbox list on its own is it possible ? eg .Software on the navbar,If clicked on it displays only the checkbox list and user can choose accordingly. Iam not creating a user input form where any details are being entered.

Many thanks

Well, it doesn’t make sense to me when you say “just display a checkbox list, without collecting user input”.
When you are collecting some input from the user, then there should be a form, even if you won’t save the result to db. IMO you should consider creating a simple model class extending Model that doesn’t have any relation to db.

Guide > Getting Started > Working with Forms
https://www.yiiframework.com/doc/guide/2.0/en/start-forms

Understood,Thanks

1 Like