Lack of array support for listbox, checkbox in CodeIgniter version 1.6.3

For those people still having one of your sites using CodeIgniter version 1.6.3 and below, you will certainly informed that CI doesn't support validation of listbox (select with multiple enabled) and checkbox for reselecting multiple values chosen. However, there is a decent hack available to enable such requirement, elaborated as below:

You should apply this patch ONLY IF you're using CI version 1.6.3 and below, CI version 1.7.0 and above uses a new form validation class and it does support array as field names.

1. Modify the core Validation.php located under system/libraries/Validation.php, then replace 2 sets of function called as "set_checkbox" and "set_select".

  1. function set_checkbox($field = '', $value = '')
  2. {
  3. if ($field == '' OR $value == '' OR ! isset($_POST[$field]))
  4. {
  5. return '';
  6. }
  7.  
  8. if(is_array($_POST[$field]))
  9. {
  10. if(in_array($value,$_POST[$field]))
  11. {
  12. return ' checked="checked"';
  13. }
  14. }
  15. elseif ($_POST[$field] == $value)
  16. {
  17. return ' checked="checked"';
  18. }
  19. }

  1. function set_select($field = '', $value = '')
  2. {
  3. if ($field == '' OR $value == '' OR ! isset($_POST[$field]))
  4. {
  5. return '';
  6. }
  7.  
  8. if(is_array($_POST[$field]))
  9. {
  10. if(in_array($value,$_POST[$field]))
  11. {
  12. return ' selected="selected"';
  13. }
  14. }
  15. elseif ($_POST[$field] == $value)
  16. {
  17. return ' selected="selected"';
  18. }
  19. }

 

2. Pay a closer attention on field name & id on listbox (select with multiple enabled) being displayed on the view file:

  1. <select id="business[]" name="business[]" size="10">
  2. </select>

Afterwards, $this->validation->set_select('business', $value) will be used to reselect the chosen values. At this point, if validation fails and the form is being reloaded to the user, then the multiple values previouly submitted will be reselected.

3. Last but not least, using $yourownvariable = $_POST["business"]; on the controller file will grab the array of submitted values and store it inside your own defined variable.

Comments

Hi, i think you’ve made some

Hi,

Hi, i think you’ve made some

Hi, i think you’ve made some truly interesting points. Not too many people would actually think about this the way you just did. I’m really impressed that there’s so much about this subject that’s been uncovered and you did it so well, with so much class. Good one you, man! Really great stuff here.mcp exam// mcse exam// mcitp exam// mcts exam// microsoft exam// a+ exam// security+ exam// network+ exam//

Hi, i think you’ve made some

Hi

A wealthy man Oliver and his

A wealthy man Oliver and his friend Dylan came to a city. Oliver said to Dylan: “You know, the city has saved my young life. That year I passed from here. buy facebook fans

The girls wondered whether

The girls wondered whether that was Jerry's bowling ball in that garbage bag or a severed head. thesis writing services

Hello there! This is my first

Hello there! This is my first comment here so I just wanted to give a quick shout out and tell you I genuinely enjoy reading through your articles. Can you recommend any other blogs/websites/forums that deal with the same subjects? Thanks a ton!Pass4sure VCP-410 Pass4sure 220-701 Pass4sure 220-702 Pass4sure 640-553 Pass4sure 350-030 Pass4sure N10-004 Pass4sure PMI-001 Pass4sure 646-205

No matter how frightening it

No matter how frightening it can be, the fact is, you need asset management in your life if you want to remain problem-free for your entire life. professioanl essay writers

This is especially true with

This is especially true with people who are about to retire and have no other means of income but the money that they have saved over the years. But no matter how frightening it can be, the fact is, you need asset management in your life if you want to remain problem-free for your entire life. Thanks for information.Web Design Quote

I am aware of the hard work

I am aware of the hard work it requires to create an informative write-up similar to this. Thank you so much for sharing with us your effective work.toronto image consultant

Wow this is good article is!

Wow this is good article is! I like it. I will share with my friends. I hope that many people also have hobby the same as me. Thank Q.rv toronto