Repopulating checkboxes in Codeigniter after Unsuccessful Form Validation

前端 未结 9 766
梦毁少年i
梦毁少年i 2021-02-04 16:18

I have a problem repopulating a set of checkboxes after an unsuccessful form validation returns the user back to the same form. Dropdown menus and text inputs could be repopulat

相关标签:
9条回答
  • 2021-02-04 16:30

    In order for set_checkbox to work properly, an actual validation rule needs to be used on that element. I ran into this problem and could not get the value to show on a resubmit until I included:

    $this->form_validation->set_rules('checkbox_name', 'checkbox_title', 'trim');
    

    Then, everything worked perfect.

    0 讨论(0)
  • 2021-02-04 16:32

    I actually found that it only works if you use like this:

    form_checkbox('ambience[]', 'value', set_checkbox('ambience[]', 'value'));
    

    You need the square array brackets on the name for it to work properly.

    0 讨论(0)
  • 2021-02-04 16:35

    You set_checkbox calls are wrong. When you're using an array like "ambience[]" in form_checkbox, you don't want to include the square brackets ([]) in your set_checkbox call. The other problem is that set_checkbox requires a second parameter which you've only included in the first 2 checkboxes.

    The set_checkbox should always be like this:

    set_checkbox('ambience', 'value');
    

    Where 'value' is the second parameter of the form_checkbox call. Like this:

    form_checkbox('ambience[]', 'value', set_checkbox('ambience', 'value'));
    
    0 讨论(0)
  • 2021-02-04 16:40

    I tried all the solutions here none worked. So i collected all the data, packed it into an array, then use a loop to check if the values in the array match the value of the selected box, if so change the checked attribute to checked.

    Here is the html code:

    <div class="col-sm-10 tags">
        <label>
            <input class="tags" type="checkbox" name="tags[]" value="love" >Love                            
        </label>
        <label>
            <input class="tags" type="checkbox" name="tags[]" value="God" >God
        </label>
        <label>
            <input class="tags" type="checkbox" name="tags[]" value="Reality" >Reality
        </label>
        <label>
            <input class="tags" type="checkbox" name="tags[]" value="Entrepreneurship">Entrepreneurship
        </label>
    </div>
    

    Here is the javascript code in a function

    (function(){
        var tag_string = '<?php echo $_post['tags']; ?>',
            tags = tag_string.split(', ');
        boxes = document.getElementsByClassName('tags');
        for(i = 0; i< boxes.length;i++ ){
            if(tags.toString().includes(boxes[i].value)){
                boxes[i].checked = "checked";
            }
        }
    })();
    
    0 讨论(0)
  • 2021-02-04 16:41
    function set_checkbox_array($field, $value)
    {
        $arr = isset($_POST[$field]) ? $_POST[$field] : FALSE;
        return ($arr !== FALSE && is_array($arr) && in_array($value, $arr)) ? 'checked="checked"' : '';
    }
    
    0 讨论(0)
  • 2021-02-04 16:47

    Here's a working example. You are required to include the array name with brackets [] in each of $this->form_validation->set_rules(), form_checkbox() and set_checkbox().

    In the controller:

        $this->load->library('form_validation');
    
        $this->form_validation->set_rules('set_reminder_days[]', 'Reminder Day', 'trim');
    
        if( $this->form_validation->run() == FALSE ) //Field validation failed.
        {
            //form validation errors will show up automatically
        }
        else //Validation success.
        {
            //This is an array of all checked checkboxes
            $reminder_days = $this->input->post('set_reminder_days');
        }
    

    In the view:

        $day_options = array(
            'S' => 'Sunday',
            'M' => 'Monday',
            'T' => 'Tuesday',
            'W' => 'Wednesday',
            'Th' => 'Thursday',
            'F' => 'Friday',
            'Sa' => 'Saturday'
        );
    
        foreach( $day_options as $key => $day_option ) {
            echo form_checkbox('set_reminder_days[]', $key, set_checkbox('set_reminder_days[]', $key), 'class="form-checkbox"');
        }
    
    0 讨论(0)
提交回复
热议问题