CheckboxList values always null

Hi.

I created a checkbox list in the view, but when I save the model, the field of the model is always null…




   <?= $form->field($advertiser, 'languageList')->checkboxList(LangaugeComponent::languages()) ?>



This is okay, I can see the checkbox list, but when I save the model, the field will be null:




   // This is NULL

   var_dump($model->languageList);



After the load function calling it is null…




   $model->load(Yii::$app->request->post());



And I cannot save this information into the Database.

How can I save checkbox list into the $languageList field?

When I post the form this is in the post variable:




    ["languageList"]=> array(2) { [0]=> string(2) "en" [1]=> string(2) "sr" }



But it seems this is not compatible with the Yii’s load function :(

For a CheckBoxList (and also for a ListBox with multiple selection) in an ActiveForm, the target attribute of the model must be an array.

Is your ‘LanguageList’ attribute a string? If so, create an additional attribute in your model that handles the language list in an array. You can populate it with the value of ‘LanguageList’ when you load it from db, and write the values back to ‘LanguageList’ before you save it to db.

Not, it is an array: public $languageList = [];

This is so interesting the both for me.

However thanks for the answer, but I still do not know what I can do with this problem, maybe I should avoid to use checkboxlist.

Can you show your model code and the controller code?

Especially how do you load/save languageList from/to db? And what do you have for validating it?

I figured out that the problem was the ‘languageList’ was missed from the rules… after I added the ‘languageList’ to the rules then it worked.




[['languageList'], 'safe'],



The rule may not be safe enough. Consider using ‘each’ validator. http://www.yiiframework.com/doc-2.0/yii-validators-eachvalidator.html