We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to set checked multiple checkboxes?

Hi. First, I see this topic: https://forum.phalcon.io/discussion/849/tag-setdefault-for-multiselect-checkbox-really-

But it not work in my case. I have Article and many categories for this article. (hasMany) I want set checkboxes cheked relation from data in db when I edit Article.

In view

<div class="form-group">
{% for category in categories %}
  <div class="checkbox">
    <label>
      {{ check_field('categories_ids[]', 'value': category.id) }} {{category.title}}
    </label>
  </div>
  {% endfor %}
</div>

In controller i trying to set like this:

$this->tag->setDefault("categories_ids[]", [1, 2]); // error: must be scalar
$this->tag->setDefaults(["categories_ids[]" => [3,4]]); // nothing

What I do wrong?

im afraid you should define a customed tag for that purpose.

follow THIS.