jeudi 28 octobre 2021

How to prevent a check_box_tag from passing it's state along with the values?

I'm working on a form with lots of checkboxes where I implemented some Javascript to have the classic "select all checkboxes" feature.

For this, I have a "master" checkbox that checks it's children check_box_tag's, and also individual check_box_tag's (the children checkboxes) for each element in the iteration block.

html:

  <div data-controller="checkbox-select-all">
    <span class="text-center">
      <%= check_box_tag "coupon[constraint_values][]", nil, false, {
        "data-action": "change->checkbox-select-all#toggleAllCheckboxes",
        "data-checkbox-select-all-target": "selectAll"
          } %> # master checkbox
    </span>

    <% current_account.plans.each do |plan| %>
      <div>
        <span class="text-center">
          <%= check_box_tag "coupon[constraint_values][]", plan.id, false, {
                            "data-checkbox-select-all-target": "checkbox",
                            "data-action": "change->checkbox-select-all#updateSelectAllCheckboxState"} %> # child checkbox
        </span>
        <%= plan.name %>
      </div>
    <% end %>
  </div>

The results of checking only children checkboxes is:

Parameters:

{ 
  "coupon"=> {
    "constraint_values"=>["25", "49", "50"],
  }
}

However, when I check a master checkbox (which makes all it's children checkboxes checked), the master checkbox state "on" is passed along it's children values:

Parameters:

{ 
  "coupon"=> {
    "constraint_values"=>["on", "25", "49", "50"],
  }
}

Is there any way to prevent this? Why is it passing it's state when the children checkboxes do not? They are built the same.




Aucun commentaire:

Enregistrer un commentaire