Timo.Klement
Timo.Klement

Reputation: 637

How to validate at least one checkbox in Magento?

I have a dynamic form with minimum one checkbox as an array.

<form action="url" method="post" id="whatever">
  <input class="checkbox" type="checkbox" name="item_ids[]" value="101" />
  <input class="checkbox" type="checkbox" name="item_ids[]" value="102" />
  <input class="checkbox validate-one-required" type="checkbox" name="item_ids[]" value="103" />
  <button type="submit">
</form>

<script type="text/javascript">
    //< ![CDATA[
    var customForm = new VarienForm('whatever');
    //]]>
</script>

I tried to use the hint from several forums to add the "validate-one-required" to the last checkbox in the group. But when I try to submit my form and I checked the first checkbox Magento told me that I have to select at least one option. Funny! I guess I did it.

Is there something else especially in the javascript area I had to add?

Upvotes: 1

Views: 3688

Answers (1)

dmanners
dmanners

Reputation: 2072

Try attach the class validate-one-required-by-name to all the input fields that you need to validate as long as they have the same name.

Upvotes: 4

Related Questions