Welcome Guest, Not a member yet? Register   Sign In
Set_checkbox and populate data
#1

[eluser]abmcr[/eluser]
I have try to use the buildin function set_checkbox() for managing the post data and the prepopulate data... and i have not success...
I have write my set_checkbox function , extending the core librarie
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
*
*/
class MY_Form_validation extends CI_Form_validation
{
    function MY_Form_validation()
    {
        parent::CI_Form_validation();
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Set Checkbox
     *
     * Enables checkboxes to be set to the value the user
     * selected in the event of an error, or to value from DB
     *
     * @access    public
     * @param    string
     * @param    string
     * @return    string
     */    
    function set_checkbox($field = '', $value = '', $default = FALSE)
    {
        
        if (isset($_POST[$field])) {
            return ' checked="checked"';
        }
    
        if (count($_POST)==0){
            if($value==$default) {
                return ' checked="checked"';
            }else{
            return '';
            }    
         }
    }
}
?>
In the view file
simple type this

Code:
<input type="checkbox" name="attivo" value="y" <?php echo set_checkbox('attivo', 'y',$row->attivo); ?> />

where $row->attivo if the value from db.....

Any suggestion for a use of core function?
#2

[eluser]crnalajna[/eluser]
Thanks, you help me.

Bug is still there (CI 1.7.1).
#3

[eluser]crnalajna[/eluser]
Actually I find out that repopulating working fine if it set default empty validation for every field.

Code:
$vars = array($field1, $field2, $field3...);
foreach($vars as $var)
    $this->form_validation->set_rules($var, '', '');




Theme © iAndrew 2016 - Forum software by © MyBB