Welcome Guest, Not a member yet? Register   Sign In
Database populated form fields (e.g. edit mode) and form validation
#6

[eluser]huston[/eluser]
I am new to CodeIgniter and was frustrated by this same issue. I have been testing a different approach which seems to be working so far.

First I extended the CI_Form_validation class to setup the _field_data when the request is not a POST. Then I added a method, set_default_value, to set the default values for the fields in _field_data.

Code:
class MY_Form_validation extends CI_Form_validation {

    function CI_Form_validation($rules = array()) {
        parent::CI_Form_validation($rules);
    }

    function set_default_value($data, $value = null) {
        if (is_array($data)) {
            foreach ($this->_field_data as $field => $row) {        
                if ($row['is_array'] == TRUE) {
                    $this->_field_data[$field]['postdata'] = $this->_reduce_array($data, $row['keys']);
                } else {
                    if (isset($data[$field]) AND $data[$field] != "") {
                        $this->_field_data[$field]['postdata'] = $data[$field];
                    }
                }
            }
        } else {
            if (isset($this->_field_data[$data])) {
                $this->_field_data[$data]['postdata'] = $value;
            }
        }
    }

    function set_rules($field, $label = '', $rules = '')
    {
        if (count($_POST) != 0) {
            parent::set_rules($field, $label, $rules);
        } else {
            if (is_array($field)) {
                foreach ($field as $row) {
                    if ( ! isset($row['field'])) {
                        continue;
                    }

                    $this->set_rules($row['field']);
                }
                return;
            }

            if ( ! is_string($field)) {
                return;
            }

            $this->_field_data[$field] = array('postdata' => NULL);
        }
    }
}

Then I added a call to set_default_value to my controller to set the current value for a field.

Code:
$this->form_validation->set_default_value('email', '[email protected]');

Here is the example from the CI Form Validation documentation with this code added.

form.php:
Code:
class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

        $this->form_validation->set_default_value('email', '[email protected]');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
}

And the code for the view remains unchanged from the CI documentation.

myform.php:
Code:
<html>
<head>
<title>My Form</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open('form'); ?>

<h5>Username</h5>
&lt;input type="text" name="username" value="&lt;?php echo set_value('username'); ?&gt;" size="50" /&gt;

<h5>Password</h5>
&lt;input type="text" name="password" value="&lt;?php echo set_value('password'); ?&gt;" size="50" /&gt;

<h5>Password Confirm</h5>
&lt;input type="text" name="passconf" value="&lt;?php echo set_value('passconf'); ?&gt;" size="50" /&gt;

<h5>Email Address</h5>
&lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?&gt;" size="50" /&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;


Messages In This Thread
Database populated form fields (e.g. edit mode) and form validation - by El Forum - 02-24-2009, 11:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB