Welcome Guest, Not a member yet? Register   Sign In
Prepopulate form data issue
#1

[eluser]TommyHot[/eluser]
Hello,

I'm requesting data from a database and inserting it into form_input, but after I send the form, I get these strange errors:
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined index: is_array
Filename: libraries/Form_validation.php
Line Number: 325

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined index: rules
Filename: libraries/Form_validation.php
Line Number: 337

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined index: is_array
Filename: libraries/Form_validation.php
Line Number: 544

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined index: field
Filename: libraries/Form_validation.php
Line Number: 558

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined index:
Filename: libraries/Form_validation.php
Line Number: 558

and so on..

My code is as following:

Code:
<?php
// application/libraries/MY_form_validation.php  
if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {
        
    function MY_Form_validation($config = array())
    {
        parent::CI_Form_validation($config);
    }
    
    function set_default_values($field, $default = '')
    {
        
        if( is_array($field) )
        {
            foreach( $field as $key => $value )
            {        
                if( !@$_POST[$key] )
                {
                    $this->_field_data[$key]['postdata'] = $value;
                }
                else
                {
                    $this->_field_data[$key]['postdata'] = $_POST[$key];
                }
            }
        }
        else
        {
            if( !@$_POST[$field] )
            {
                $this->_field_data[$field]['postdata'] = $default;
            }
            else
            {
                $this->_field_data[$field]['postdata'] = $_POST[$field];
            }
        }
    }
}
?>

Code:
<?php
// controller file

class Clients extends Controller {
...
...
    function edit()
    {
        $this->load->library('form_validation');
        $this->load->helper('form');
        
        $data['title'] = 'Edit a Client';
        $data['page'] = "{$this->router->class}_cu";
        
        $this->db->select('client_id, client_name, client_email, client_phones, client_country, client_theme');
        $this->db->where('client_id', $this->uri->segment(4));
        $query = $this->db->get('{_}clients');
        
        $row = $query->row();
        $data['row'] = $row;
        
        $this->form_validation->set_default_values(
             array(
                 'name' => $row->client_name,
                 'email' => $row->client_email,
                 'phones' => $row->client_phones,
                 'theme' => $row->client_theme,
             )
         );

        if( $this->input->post('submit') )
        {    
        
            if( $this->form_validation->run('clients') == TRUE )
            {
                // some stuff
            }
        }
        
        $this->load->view('backend/backend_template',$data);
    }

}

Code:
//view file
...
...
<?=form_input(array('name' => 'name', 'id' => 'name'), set_value('name'));?>
<?=form_input(array('name' => 'name', 'id' => 'email'), set_value('email'));?>
<?=form_input(array('name' => 'name', 'id' => 'phones'), set_value('phones'));?>
<?=form_input(array('name' => 'name', 'id' => 'theme'), set_value('theme'));?>

All I want to do, is to pass an array (or 2 strings /field and value/) with fields and values and pass it to the set_default_values() method.

View file should at first outputs database items, and after you change the value of form_input and resubmit the form, it should use the _POST value instead of the database one.
#2

[eluser]cahva[/eluser]
Hmm.. Dont know why you took the trouble of extending the form validation class as you could easily do that with the default setup using the second parameter for set_value:
Code:
<?=form_input(array('name' => 'name', 'id' => 'name'), set_value('name',$row->client_name)) ?>

With that it will first put the default value from db and after submitting the form, it will use posted items. And as always, its good practise to use redirect on successfull submission(update db && redirect to same page for example).




Theme © iAndrew 2016 - Forum software by © MyBB