4
votes

When I try to populate a form field which is not required in form validation doesn't repopulate. Let's say I have a field but I don't want it to be required but I want it to be repopulated. How can I do it ? I think this is a bug for codeigniter.

3
What do you mean by repopulated? Do you mean you want to set the value of a field after a from is submitted but not valid? - Matthew Rapati
I want to repopulate a field which is not set as required in form validation rules. In form validation if you don't set a rule as required It doesn't populate it. - SNaRe
BTW; I think I solved the problem with a hack with helpers. But I'm really wondering a real solution. - SNaRe

3 Answers

4
votes

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;
} 
3
votes

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.

1
votes

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'));