I think there is a bug in codeigniter form validation class.
My code is working perfectly fine if value is greater than zero but if it’s “0” set_value function is not working:
Controller:
$this->form_validation->set_rules('age', 'Age', 'required|is_natural');
View:
<? if(set_value('age')) { ?>
<input id="age" name="age" type="text" value="<?=set_value('age')?>" />
<? } else { ?>
<input id="age" name="age" type="text" value="Age" />
<? } ?>
Am i doing something wrong or is this a bug ?
Your form field is not being repopulated when the value is
0because yourifstatement evaluates it toFALSEand then outputs the form field without thevalueattribute.So, get rid of the
ifstatement. You simply want this instead:set_value()will either return the value (if one is provided), and either the default value or an empty string if no value is provided.