When using CodeIgniter's Form Validation class, the form_errors
in the view always show the error messages. Also, using the set_value
method never populates the field with the correct values unless there's no validation against it.
I have extended the form_validation class, but only to add a new method that I needed and removing the code entirely does not make a difference.
The code for my controller is:
function test() {
$data['title'] = 'My title';
$this->form_validation->set_rules('title', 'Title', 'trim|required');
if($this->form_validation->run() == FALSE) {
$this->load->view('form', $data);
} else {
$this->load->view('form', $data);
}
}
The code for my view is:
echo form_open(current_url()).
form_error('title').
form_label('Title', 'title').
form_input(array('name' => 'title', 'id' => 'title'), set_value('title', $title)).
form_label('Other', 'other').
form_input(array('name' => 'other', 'id' => 'other'), set_value('other', 'test')).
form_submit('submit', 'Update').
form_close();
What I expect is that there are no error messages show, the first input field would have the text 'My title' and the second field would have the text 'other'. If I submitted the forms, then they would show the posted values.
In reality, I always get an error message saying that the first field is required, the first field is always empty when the page loads and the second field is filled correctly with the text 'other'.
If I fill in both/either of the fields and post the form, the fields are returned with the post data, which is correct. I cannot see what I am doing wrong and any help is appreciated.