How to Repopulate a form field without setting rule as required in Codeigniter?
This piece of code solved my problem. But I have to use helpers for that. At least I have a solution.
function value_field($field, $default='') { return (isset($_POST[$field])) ? $_POST[$field] : $default;}
You have to set a validation rule on a field in order for it to repopulate on an invalid submission. Luckily, you can pass regular PHP functions, not just validation rules, to the set_rules()
method so you could just set trim or something rather than required.
$this->form_validation->set_rules('your_field', 'Your Label', 'trim');
That will repopulate the field and not make it a required field.
Using the form helper's set_value() works even for non-required fields, I'm not sure where your problem lies...
echo form_input(array('name' => 'username', 'value' => set_value('username'));