Codeigniter validate checkbox group
11,584
Looking at your code you set options[]
as your name in set_rules
:
$this->form_validation->set_rules('options[]','options', 'required');
while on your HTML your checkbox name is option[]
<input type="checkbox" class="threads" name="option[]" value="<?php echo $thread['th_id']; ?>" />
Fix this and tell me what the error is and i'll update my answer
Author by
Mamoum Elsir
working as php developer in knowledge developers in Riyadh KSA looking forward to be useful for everyone ^_^
Updated on June 26, 2022Comments
-
Mamoum Elsir almost 2 years
I have this html code that I want to validate using the Codeigniter
Form_Validation
class :<form id="thread" method="POST"> <input type="checkbox" name="checkAllAuto" id="checkAll" onclick="CheckAll(this.id)" /><?php echo __t($dir_lang,'request_select_all');?> <br /> <?php foreach ($threads as $thread): ?> <input type="checkbox" class="threads" name="option[]" value="<?php echo $thread['th_id']; ?>" /> <?php echo $thread['th_title']; ?> <br> <?php endforeach; ?> <br /> <input type="checkbox" name="mom" /> <?php echo __t($dir_lang,'request_send');?> <br /> <select name="cat"> <?php foreach ($cats as $cat): ?> <option value="<?php echo $cat['id']; ?>"> <?php echo $cat['title']; ?> </option> <?php endforeach; ?> </select> <br /><input type="submit" value="<?php echo __t($dir_lang,'submit');?> " /><br> </form>
I'm trying to validate the fist group of the check box by using
Form_Validation
if ($this->input->isMethodPost()) { $this->tplData['showMess'] = true; $this->load->library('form_validation'); $this->lang->load('form_validation', 'english'); $this->form_validation->set_error_delimiters('<h3>', '</h3>'); $this->form_validation->set_rules('options[]','options', 'required'); if ($this->form_validation->run() == FALSE) { $this->tplData['showError'] = true; $this->tplData['mess'] = array(validation_errors()); }else{ // my work } }
It gives required filed message but even if I choose one or all the checkbox it still gives the required message.
-
Hana90 about 7 yearsfor me it only worked if I use the following: $this->form_validation->set_rules('option[]','option[]', 'required'); <input type="checkbox" class="threads" name="option[]" value="<?php echo $thread['th_id']; ?>" />
-
Hana90 about 7 yearsThis is for CodeIgniter 3.1.2