mirza
mirza

Reputation: 5793

Codeigniter Validation Error While Using 0 value in an input

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 ?

Upvotes: 0

Views: 1696

Answers (1)

Colin Brock
Colin Brock

Reputation: 21565

Your form field is not being repopulated when the value is 0 because your if statement evaluates it to FALSE and then outputs the form field without the value attribute.

So, get rid of the if statement. You simply want this instead:

<input id="age" name="age" type="text" value="<?=set_value('age')?>" />

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.

Upvotes: 1

Related Questions