Checkbox Array Validation Codeigniter -
on form each row has it's on submit button , need check check box before can delete else should through error.
question: checkbox in_array if not check box , press submit not through codeigniter form_validation error. have used $this->form_validation->set_rules('selected[]', 'selected', 'required');
error not showing up.
what best solution in making getting form_validation error work?
view
<?php echo form_open('admin/design/layout/delete'); ?> <?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?> <div class="table-responsive"> <table class="table table-striped table-bordered table-hover"> <thead> <tr> <td style="width: 1px;" class="text-center"> <input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /> </td> <td>layout name</td> <td class="text-right">action</td> </tr> </thead> <tbody> <?php if ($layouts) { ?> <?php foreach ($layouts $layout) { ?> <tr> <td class="text-center"><?php if (in_array($layout['layout_id'], $selected)) { ?> <input type="checkbox" name="selected[]" value="<?php echo $layout['layout_id']; ?>" checked="checked" /> <?php } else { ?> <input type="checkbox" name="selected[]" value="<?php echo $layout['layout_id']; ?>" /> <?php } ?> </td> <td><?php echo $layout['name']; ?></td> <td class="text-right"> <button type="submit" class="btn btn-danger">delete</button> <a href="<?php echo $layout['edit']; ?>" class="btn btn-primary">edit</a> </td> </tr> <?php } ?> <?php } else { ?> <?php } ?> </tbody> </table> </div> </div> <?php echo form_close(); ?>
controller function
public function delete() { $this->load->library('form_validation'); $this->form_validation->set_rules('selected[]', 'selected', 'required'); if ($this->form_validation->run() == false) { echo "error"; $this->get_list(); } else { $selected_post = $this->input->post('selected'); if (isset($selected_post)) { foreach ($selected_post $layout_id) { } echo "deleted $layout_id"; $this->get_list(); } } }
it won't validate per field. selected[]
selector in rules means, when submit form, should @ least 1 selected item. have submit buttons, independently submit form, no matter in dom, , checkboxes selected. same, have 1 submit button @ end.
i add javascript, , set if checkbox not selected, can disable field:
<script> $(function() { $('input:checkbox').on('change', function() { if($(this).is(':checked')) { $(this).closest('tr').find('button:submit').prop('disabled', false); } else { $(this).closest('tr').find('button:submit').prop('disabled', true); } }) }) </script>
and add disabled
submit buttons:
<button type="submit" class="btn btn-danger" disabled>delete</button>
it's not server side validation, can achieve prevent push button next unchecked checkboxes.
Comments
Post a Comment