Welcome Guest, Not a member yet? Register   Sign In
Using form_validation with other than POST data
#1

[eluser]Unknown[/eluser]
Hi, I recently needed to perform validation on data that isn't coming from $_POST. This patch allows the data which needs to be validated to be passed to the run() function as an optional second parameter. If it's not supplied, $_POST is used by default. It should be backwards compatible with existing form_validation users.

Code:
33d32
<       var $form_data                          = array();
39a39
>
77a78,83
>               // No reason to set rules if we have no POST data
>               if (count($_POST) == 0)
>               {
>                       return;
>               }
>
271c277
<       function run($group = '', $form_data = array() )
---
>       function run($group = '')
273,282d278
<               // Did the user supply an array of form data, or should we use $_POST
<               if (count($form_data) == 0)
<               {
<                       $this->form_data = $_POST;
<               }
<               else
<               {
<                       $this->form_data = $form_data;
<               }
<
284c280
<               if (count($this->form_data) == 0)
---
>               if (count($_POST) == 0)
323c319
<               // corresponding $this->form_data item and test for errors
---
>               // corresponding $_POST item and test for errors
326c322
<                       // Fetch the data from the corresponding $this->form_data array and cache it in the _field_data array.
---
>                       // Fetch the data from the corresponding $_POST array and cache it in the _field_data array.
331c327
<                               $this->_field_data[$field]['postdata'] = $this->_reduce_array($this->form_data, $row['keys']);
---
>                               $this->_field_data[$field]['postdata'] = $this->_reduce_array($_POST, $row['keys']);
335c331
<                               if (isset($this->form_data[$field]) AND $this->form_data[$field] != "")
---
>                               if (isset($_POST[$field]) AND $_POST[$field] != "")
337c333
<                                       $this->_field_data[$field]['postdata'] = $this->form_data[$field];
---
>                                       $this->_field_data[$field]['postdata'] = $_POST[$field];
368c364
<        * Traverse a multidimensional $this->form_data array index until the data is found
---
>        * Traverse a multidimensional $_POST array index until the data is found
403c399
<        * Re-populate the this->form_data array with our finalized and processed data
---
>        * Re-populate the _POST array with our finalized and processed data
416c412
<                                       if (isset($this->form_data[$row['field']]))
---
>                                       if (isset($_POST[$row['field']]))
418c414
<                                               $this->form_data[$row['field']] = $this->prep_for_form($row['postdata']);
---
>                                               $_POST[$row['field']] = $this->prep_for_form($row['postdata']);
424c420
<                                       $post_ref =& $this->form_data;
---
>                                       $post_ref =& $_POST;
472c468
<               // If the $this->form_data data is an array we will run a recursive call
---
>               // If the $_POST data is an array we will run a recursive call
900c896
<               if ( ! isset($this->form_data[$field]))
---
>               if ( ! isset($_POST[$field]))
905c901
<               $field = $this->form_data[$field];
---
>               $field = $_POST[$field];
#2

[eluser]xwero[/eluser]
It seems like a clever solution because you can have multiple runs which is very likely if you can validate more than one input source. Thank you for contributing
#3

[eluser]Unknown[/eluser]
This is great!

Could you post the entire file so making these changes incrementally isn't necessary? I think I'm just going to override my core lib with this one.




Theme © iAndrew 2016 - Forum software by © MyBB