0

On my form each row has it's on submit button and you need to check the check box before you can delete it else it should through error.

Question: My checkbox is in_array but if I do not check the box and then press submit it does not through the codeigniter form_validation error. I have used $this->form_validation->set_rules('selected[]', 'Selected', 'required'); But error not showing up.

What is the best solution in making it getting form_validation error to 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 as $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 as $layout_id) {
            }
            echo "Deleted  $layout_id";
            $this->get_list();
        }           
    }
}
2

1 Answer 1

1

It won't validate per field. selected[] selector in rules means, when you submit your form, it should be at least one selected item. Now you have submit buttons, which are independently submit the form, no matter where are they in the dom, and which checkboxes are selected. Currently it the same, as you would have one submit button at the end.

I would add some javascript, and set if the checkbox is not selected, you can disable that 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 to your submit buttons:

<button type="submit" class="btn btn-danger" disabled>Delete</button>

It's not a server side validation, but you can achieve to prevent push the button next unchecked checkboxes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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