Repopulating checkboxes in Codeigniter after Unsuccessful Form Validation

空扰寡人 提交于 2019-12-31 17:48:15

问题


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 repopulated, but not checkboxes!

Here's a snippet of the code for the checkboxes:

        <td>
            <?php echo form_checkbox('ambience[]', 'casual', set_checkbox('ambience[]', 'casual')); ?> Casual <br />
            <?php echo form_checkbox('ambience[]', 'romantic', set_checkbox('ambience[]', 'romantic')); ?> Romantic <br />
            <?php echo form_checkbox('ambience[]', 'outdoor', set_checkbox('ambience[]')); ?> Outdoor / Alfresco <br />
            <?php echo form_checkbox('ambience[]', 'trendy', set_checkbox('ambience[]')); ?> Hip & Trendy <br />
            <?php echo form_checkbox('ambience[]', 'vibrant', set_checkbox('ambience[]')); ?> Vibrant <br />
            <?php echo form_checkbox('ambience[]', 'up_scale', set_checkbox('ambience[]')); ?> Upscale <br />
        </td>

The code snippet for text input which successfully repopulated is:

<?php echo form_dropdown('price_range', $options, set_value('price_range')); ?>

Any ideas? I'm really confused why set_checkbox does not work as advertised.


回答1:


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'));



回答2:


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.




回答3:


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.




回答4:


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"');
    }



回答5:


This is not use form helper. I try to use this code.

<input type="checkbox" name="tes_display" value="1" <?php echo set_checkbox('tes_display', '1', FALSE); ?> /> 



回答6:


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"' : '';
}



回答7:


I realised that set_checkbox takes 3 parameters :

 set_checkbox(string $checkboxname, string $value, boolean $isChecked);

For example :

 echo form_checkbox('mycbx[]',
                    $item['id'],
                    set_checkbox('mycbx[]', $item['id'], false)
      );

or this way :

$checkbox = array(
    'name'        => 'mycbx[]',
    'value'       => $item['id'],
    'checked'     => set_checkbox('mycbx[]', $item['id'], false)
);
echo form_checkbox($checkbox);



回答8:


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";
        }
    }
})();



回答9:


In one of my projects, I have used it as follows:

In the view I did:

$checkbox_name = 'menu_type_id[]';
$menu_type_attributes = array(
  'name'              => 'menu_type_id[]',
  'id'                => 'menu_type_id',
  'class'             => 'checkbox-custom rectangular',
  'value'             => $menu_type->get_id(), 
  'checked'           => set_checkbox('menu_type_id[]', $menu_type->get_id()),                 
  'aria-describedby'  => 'menuTypeIdHelp'
);
echo form_checkbox($menu_type_attributes);

In the controller I have the validation (FYI, you will have to have form validation on the checkbox if you want to retain the checked status of the checkbox)

$this->form_validation->set_rules('menu_type_id[]', 'Menu type(s)', 'required');

Even though your business rule does not require you to have at least one checkbox checked, you will still need to put a validation for the checkbox. For eg, you could use

$this->form_validation->set_rules('menu_type_id[]', 'Menu type(s)', 'trim'); etc.

Hope this will help some one out there, b/c this information is not available in Code Igniter's documentation.



来源:https://stackoverflow.com/questions/6338220/repopulating-checkboxes-in-codeigniter-after-unsuccessful-form-validation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!