Welcome Guest, Not a member yet? Register   Sign In
Re-populating form and hack
#1

[eluser]dhenoer[/eluser]
Re-populating the form after form_validation returning false, make our application seems more smart, because all value are restored again, and ready to fix.

The problem arise when we 'edit' a set of data, so the input values are gotten from database. I mean how to display their initial value. I found a simple method that I think a hack to inject initial data.

In the controller, I create a method like this:

Code:
function edit($id){
    $this->load->model('testmodel');
    $this->load->library('form_validation');
    $this->load->helper('form');

    if ($this->input->post('submit')) {

        $this->form_validation->set_rules('ID','Identity','required');
        $this->form_validation->set_rules('Name','Name','required|min_length[5]');

        if ($this->form_validation->run()){
            $data['propID'] = $this->input->post('ID');
            $data['propName'] = $this->input->post('Name');
            $this->testmodel->update($data, $id);
    
            die('success!');
        }
    
    } else {
        $data = $this->testmodel->getByID($id);
    }

    $this->load->view('test/edit', @$data); //i have to use @ to hide notice warning
}

and code of the view as follows:
Code:
<h1>Edit</h1>

&lt;?= form_open($this->uri->uri_string()) ?&gt;

Id: &lt;?= form_input('ID', set_value('ID', @$propID)) ?&gt;<br/>
Name: &lt;?= form_input('Name', set_value('Name', @$propName)) ?&gt;<br/>
&lt;?= form_submit('submit', 'Submit!') ?&gt;
&lt;?= form_close()?&gt;

What we do is: prepend '@' before $data to send to view from controller.
And prepend '@' before each fieldnames as default value in your view.
This method hides warning notice at that line, but your application still runs.


Messages In This Thread
Re-populating form and hack - by El Forum - 12-10-2009, 03:30 AM
Re-populating form and hack - by El Forum - 12-10-2009, 04:57 AM
Re-populating form and hack - by El Forum - 12-10-2009, 07:28 AM
Re-populating form and hack - by El Forum - 12-10-2009, 07:38 AM
Re-populating form and hack - by El Forum - 12-10-2009, 06:27 PM
Re-populating form and hack - by El Forum - 12-11-2009, 01:12 AM
Re-populating form and hack - by El Forum - 12-11-2009, 04:10 AM
Re-populating form and hack - by El Forum - 12-11-2009, 04:16 AM
Re-populating form and hack - by El Forum - 12-11-2009, 04:18 AM



Theme © iAndrew 2016 - Forum software by © MyBB