0

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 ?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.