1

I have shown multiple check boxes,when I submit need atleast one checkbox is required., i tried below code.,

$fieldset->addField('checkboxes', 'checkboxes', array(
            'label'     => Mage::helper('listings')->__('Select Categories'),
            'name'      => 'cat_ids[]',
            'values'    => $categorieslist,
            'onclick'   => "",
            'onchange'  => "",
            'value'  => $cat_ids,
            'disabled' => true,
            'class' => 'category_match_to_listing',
            'required' => true,
        ));

even class name also not displayed in html

<input type="checkbox" value="14" name="cat_ids[]" id="checkboxes_id">

if i get one value 'values' => 'single', required field working fine,then the html is

<input type="checkbox" value="0" class="category_match_to_listing required-entry" name="cat_ids[]" id="checkboxes_id">

when I get dynamic it doesn't work., please suggest any ideas?

  • Why are you passing cat_ids[] to the name parameter? – Tim Hallman May 26 '15 at 20:42
  • passing multiple checkbox value – Sathish Jun 18 '15 at 6:04
7

Replace your code with this code

$fieldset->addField('checkboxes', 'checkboxes', array(
        'label'     => Mage::helper('listings')->__('Select Categories'),
        'name'      => 'cat_ids[]',
        'values'    => $categorieslist,
        'onclick'   => "",
        'onchange'  => "",
        'value'  => $cat_ids,
        'disabled' => true,
        'class' => 'category_match_to_listing validate-one-required',
        'required' => true,
    ));
  • 1
    You can also use validate-one-required-by-name method – Vaibhav Ahalpara Feb 6 '17 at 8:47
3

Try adding the class attribute like this:

'class' => 'category_match_to_listing validate-one-required-by-name',
  • I tried that Even though I didn't get class attribute in O/P., – Sathish Feb 11 '15 at 12:57
3

To Select atleast one checkbox try below code

<input type="checkbox" value="14" class="validate-one-required" name="cat_ids[]" id="checkboxes_id" />

it worked for me..

2

I suspect your issue is with $cat_ids. What happens when you var_dump($cat_ids) when getting your error? Does it return null or a string, rather than an array?

I'd suspect you're passing a non-array with 'value' => $cat_ids which is creating the problem.

If that's the case, you can try something like:

if (!is_array($cat_ids)) (array) $cat_ids;

Or, a bit easier to read:

if ( !is_array($cat_ids)) { // if $cat_ids is NOT an array
    $cat_ids = array($cat_ids); // turn it into an array
}
  • I think you didn't understand my question, I use 'required' => true but it doesn't shown in frontend – Sathish May 27 '15 at 5:45
  • I haven't had my coffee yet... but considering a checkbox is a boolean, using 'required' isn't going to work. You'll need some code to validate all the boxes, making sure at least one is checked. – Tim Hallman May 27 '15 at 12:39
1

Replace

<input type="checkbox" value="0" class="category_match_to_listing required-entry" name="cat_ids[]" id="checkboxes_id">

With

<input type="checkbox" value="0" class="category_match_to_listing validate-one-required-by-name" name="cat_ids[]" id="checkboxes_id">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.