![]() |
An in_array() solution for form validation - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: An in_array() solution for form validation (/showthread.php?tid=20157) |
An in_array() solution for form validation - El Forum - 06-30-2009 [eluser]Unknown[/eluser] Hey, I've been using CodeIgniter for a couple of days now as framework of a rather large application interacting with a couple of VoIP servers. So far it's been great. However, I found one limitation in the form validation class. I like to filter a value of a selectbox against a whitelist array. In its simplest form: Code: <?php I discovered the Form_validation library has no such feature, so I decided to create one myself, which I would like to share with you all. Perhaps there's a better solution which one of you would want to share with me, instead? First of all, I created a file in system/application/libraries called My_Form_validation.php: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Basically, the comments should say it all. When using: Code: $this->form_validation->set_rules('servers', 'servers', 'is_in_array['.$white_list.']'); $white_list can't be an array. That's why I created a function that will turn it into a comma seperated string, which should be called like this: Code: $white_list = $this->form_validation->prepare_array($servers); Setting the 2nd parameter to false (default is true), $white_list will contain the array of just the values, so you don't need two seperate functions (in_array() and array_key_exists()). Now I'll just need to think of a workaround for whenever a value might contain a comma, cause that will obviously mess things up. Ideally, I should make the delimiter configurable. Feel free to add to this. An in_array() solution for form validation - El Forum - 06-30-2009 [eluser]Phil Sturgeon[/eluser] Use something other than a comma then. ;-) The validation library is taking care of the join and the split so you could use the following. Code: $string = implode('[!!!--SUPERDOOPERFRICKINUNLIKLY--!!!]', $array); Or possibly something a little shorter :-P An in_array() solution for form validation - El Forum - 06-30-2009 [eluser]Unknown[/eluser] Heh, yeah, I could actually just use something like that I guess ![]() |