Forms: Validating Multiple Inputs (As Array) |
[eluser]CISCK[/eluser]
I'm having some trouble validating a set of inputs, which make an array. (I have read the docs and searched the forms). Here's a simplified what I have: Code: <form action="<? site_url('welcome') ?>" method="post"> This works fine and submits an array that looks like the following (in the profiler): Code: $_POST['phone'] ... Array( Initially, I thought I could simply validate like so: Code: array( Unless I'm mistaken, this is not the case when validating arrays. So, I changed the rules key for "phone[]" to a custom rule–"valid_phone": Code: // ./system/application/libraries/MY_Form_Validation.php Here is the problem. For whatever reason, the $arr variable seems to be giving me an empty array, even if the fields are populated. Help. Please?
[eluser]toopay[/eluser]
Create a callback function, right in related controller, and validate the array inside that function. Callbacks: Your own Validation Functions
[eluser]CISCK[/eluser]
It turns out that the valid_phone (callback) function actually runs 3 times, each with a different value from the array. So, the function is actually being passed a string. Check out the example code: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); This will output the following in the view: "Not an array Not an array Not an array." So, the function is run once for each item in the array and receives a string for the argument. This is NOT ideal since I want to explode and validate the entire array in a single function. Any thoughts? Also, I want to point out that using "MY_Form_Validation.php" will accomplish the same thing as using a callback in your controller.
[eluser]toopay[/eluser]
[quote author="CISCK" date="1308894440"]Also, I want to point out that using "MY_Form_Validation.php" will accomplish the same thing as using a callback in your controller.[/quote] Off course, but there is no neccesary to extend the form validation, for something which already provided, right? Related with your problem, try this... Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
[eluser]CISCK[/eluser]
Thanks, toopay! This is helpful and has solved my problem... kinda. The valid_phone callback still runs 3 times--once for each array key. I know this does not change the accuracy of the validation, but I still wonder if there's a better way. I have lots of phone numbers, licenses, socials, etc. broken up into multiple fields, and pretty soon, the callbacks are called 20-30+ extra times. That's no good. Anyway, thanks again for your help, as passing 'phone' instead of 'phone[]' has allowed me to use this callback successfully: Code: public function valid_phone($arr = array()) |
Welcome Guest, Not a member yet? Register Sign In |