0

I am having a dynamic checkbox which is working on foreach. I want to add a validation for that. If I have added 'required' in my input type, it is asking to select all the checkboxes, Is there any other option to validate at least one checkbox is checked.

My Form

<?php 
foreach($category_list as $list) {
  if(!empty($prop_cat_check)){
    $checked = null;
    if(in_array($list->cat_id,$prop_cat_check)){
        $checked = 'checked';
      }
    }                       
    ?>                      
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>" 
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" >
<?php echo $list->cat_title; 
}
} ?> 
1

1 Answer 1

1

try this code

    <?php 
$k==0;
foreach($category_list as $list) {
  if(!empty($prop_cat_check)){
    $checked = null;
    if(in_array($list->cat_id,$prop_cat_check)){
        $checked = 'checked';
      }
    }                       
    ?>                      
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>" 
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" <?php if($k==0) echo 'required';?>>
<?php echo $list->cat_title; 
$k++;
}
} ?> 
Sign up to request clarification or add additional context in comments.

1 Comment

It works only when the first checkbox has checked,if we checked any other in the list it still says the first checkbox should check :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.