[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>
<?= form_open($this->uri->uri_string()) ?>
Id: <?= form_input('ID', set_value('ID', @$propID)) ?><br/>
Name: <?= form_input('Name', set_value('Name', @$propName)) ?><br/>
<?= form_submit('submit', 'Submit!') ?>
<?= form_close()?>
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.