summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--system/libraries/Form_validation.php54
-rw-r--r--tests/codeigniter/libraries/Form_validation_test.php18
-rw-r--r--user_guide_src/source/changelog.rst1
-rw-r--r--user_guide_src/source/libraries/form_validation.rst21
4 files changed, 64 insertions, 30 deletions
diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php
index 6b7d9893e..c1cfcdb8f 100644
--- a/system/libraries/Form_validation.php
+++ b/system/libraries/Form_validation.php
@@ -105,13 +105,6 @@ class CI_Form_validation {
protected $error_string = '';
/**
- * Whether the form data has been validated as safe
- *
- * @var bool
- */
- protected $_safe_form_data = FALSE;
-
- /**
* Custom data to validate
*
* @var array
@@ -414,10 +407,11 @@ class CI_Form_validation {
*
* This function does all the work.
*
- * @param string $group
+ * @param string $config
+ * @param array $data
* @return bool
*/
- public function run($group = '')
+ public function run($config = NULL, &$data = NULL)
{
$validation_array = empty($this->validation_data)
? $_POST
@@ -428,19 +422,19 @@ class CI_Form_validation {
if (count($this->_field_data) === 0)
{
// No validation rules? We're done...
- if (count($this->_config_rules) === 0)
+ if (empty($this->_config_rules))
{
return FALSE;
}
- if (empty($group))
+ if (empty($config))
{
// Is there a validation rule for the particular URI being accessed?
- $group = trim($this->CI->uri->ruri_string(), '/');
- isset($this->_config_rules[$group]) OR $group = $this->CI->router->class.'/'.$this->CI->router->method;
+ $config = trim($this->CI->uri->ruri_string(), '/');
+ isset($this->_config_rules[$config]) OR $config = $this->CI->router->class.'/'.$this->CI->router->method;
}
- $this->set_rules(isset($this->_config_rules[$group]) ? $this->_config_rules[$group] : $this->_config_rules);
+ $this->set_rules(isset($this->_config_rules[$config]) ? $this->_config_rules[$config] : $this->_config_rules);
// Were we able to set the rules correctly?
if (count($this->_field_data) === 0)
@@ -482,17 +476,22 @@ class CI_Form_validation {
$this->_execute($row, $row['rules'], $row['postdata']);
}
- // Did we end up with any errors?
- $total_errors = count($this->_error_array);
- if ($total_errors > 0)
+ if ( ! empty($this->_error_array))
{
- $this->_safe_form_data = TRUE;
+ return FALSE;
}
- // Now we need to re-set the POST data with the new, processed data
- empty($this->validation_data) && $this->_reset_post_array();
+ // Fill $data if requested, otherwise modify $_POST, as long as
+ // set_data() wasn't used (yea, I know it sounds confusing)
+ if (func_num_args() >= 2)
+ {
+ $data = empty($this->validation_data) ? $_POST : $this->validation_data;
+ $this->_reset_data_array($data);
+ return TRUE;
+ }
- return ($total_errors === 0);
+ empty($this->validation_data) && $this->_reset_data_array($_POST);
+ return TRUE;
}
// --------------------------------------------------------------------
@@ -580,7 +579,7 @@ class CI_Form_validation {
*
* @return void
*/
- protected function _reset_post_array()
+ protected function _reset_data_array(&$data)
{
foreach ($this->_field_data as $field => $row)
{
@@ -588,27 +587,24 @@ class CI_Form_validation {
{
if ($row['is_array'] === FALSE)
{
- isset($_POST[$field]) && $_POST[$field] = $row['postdata'];
+ isset($data[$field]) && $data[$field] = $row['postdata'];
}
else
{
- // start with a reference
- $post_ref =& $_POST;
-
// before we assign values, make a reference to the right POST key
if (count($row['keys']) === 1)
{
- $post_ref =& $post_ref[current($row['keys'])];
+ $data_ref =& $data[current($row['keys'])];
}
else
{
foreach ($row['keys'] as $val)
{
- $post_ref =& $post_ref[$val];
+ $data_ref =& $data_ref[$val];
}
}
- $post_ref = $row['postdata'];
+ $data_ref = $row['postdata'];
}
}
}
diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php
index afd070e05..ebb14983e 100644
--- a/tests/codeigniter/libraries/Form_validation_test.php
+++ b/tests/codeigniter/libraries/Form_validation_test.php
@@ -610,6 +610,24 @@ class Form_validation_test extends CI_TestCase {
$this->assertEquals('?>', $this->form_validation->encode_php_tags('?>'));
}
+ public function test_validated_data_assignment()
+ {
+ $_POST = $post_original = array('foo' => ' bar ', 'bar' => 'baz');
+
+ $this->form_validation->set_data($_POST);
+ $this->form_validation->set_rules('foo', 'Foo', 'required|trim');
+
+ $data_processed = NULL;
+ $validation_result = $this->form_validation->run('', $data_processed);
+
+ $this->assertTrue($validation_result);
+ $this->assertEquals($post_original, $_POST);
+ $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $data_processed);
+
+ $this->form_validation->reset_validation();
+ $_POST = array();
+ }
+
/**
* Run rules
*
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index c1f53dac0..db2be2014 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -52,6 +52,7 @@ Release Date: Not Released
- Changed method ``set_rules()`` to throw a ``BadMethodCallException`` when its first parameter is not an array and the ``$rules`` one is unused.
- Added rule **valid_mac**, which replicates PHP's native ``filter_var()`` with ``FILTER_VALIDATE_MAC``.
- Added ability to validate entire arrays at once, if ``is_array`` is within the list of rules.
+ - Added ability to fetch processed data via a second parameter to ``run()``.
- :doc:`HTML Table Library <libraries/table>` changes include:
diff --git a/user_guide_src/source/libraries/form_validation.rst b/user_guide_src/source/libraries/form_validation.rst
index 6a92cc983..fd2f3af94 100644
--- a/user_guide_src/source/libraries/form_validation.rst
+++ b/user_guide_src/source/libraries/form_validation.rst
@@ -881,6 +881,24 @@ When a rule group is named identically to a controller class/method it
will be used automatically when the ``run()`` method is invoked from that
class/method.
+Accessing validated/processed data
+==================================
+
+By default, validation will be performed directly on the ``$_POST`` array,
+and any possible modifications (like trimming whitespace, for example)
+would be written back onto it.
+However, if you want to keep the original input data intact, or have used
+``set_data()`` to pass a custom set of inputs, you would likely want to
+fetch the now-modified data. In order to do that, you can pass a variable
+as the second parameter to ``run()``::
+
+ $input = array('name' => ' White Space ');
+ $output = NULL;
+
+ $this->form_validation->set_rules('name', 'Name', 'required|trim');
+ $this->form_validation->run(NULL, $output);
+ // $output will now contain: array('name' => 'White Space');
+
.. _using-arrays-as-field-names:
***************************
@@ -1043,9 +1061,10 @@ Class Reference
- :ref:`setting-validation-rules`
- :ref:`saving-groups`
- .. php:method:: run([$group = ''])
+ .. php:method:: run([$config = NULL[, $data = NULL]])
:param string $group: The name of the validation group to run
+ :param mixed $data: Optional variable to assign validated data to
:returns: TRUE on success, FALSE if validation failed
:rtype: bool