summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Andreev <narf@devilix.net>2017-06-21 14:29:25 +0200
committerAndrey Andreev <narf@devilix.net>2017-06-21 14:29:25 +0200
commit56c233fc4a455d33d2e679b59132b8a7a1cf1832 (patch)
treee0071a7a0a96fa375ee6c352f25bf6d18e426585
parentf39cb00559b86f956737c62ec1dcbc9aab29ebb9 (diff)
Implement #193: Validating entire arrays in a single function call
-rw-r--r--system/libraries/Form_validation.php20
-rw-r--r--tests/codeigniter/libraries/Form_validation_test.php8
-rw-r--r--user_guide_src/source/changelog.rst1
3 files changed, 20 insertions, 9 deletions
diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php
index 6eaf67710..6b7d9893e 100644
--- a/system/libraries/Form_validation.php
+++ b/system/libraries/Form_validation.php
@@ -627,11 +627,13 @@ class CI_Form_validation {
*/
protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
{
+ $allow_arrays = in_array('is_array', $rules, TRUE);
+
// If the $_POST data is an array we will run a recursive call
//
// Note: We MUST check if the array is empty or not!
// Otherwise empty arrays will always pass validation.
- if (is_array($postdata) && ! empty($postdata))
+ if ($allow_arrays === FALSE && is_array($postdata) && ! empty($postdata))
{
foreach ($postdata as $key => $val)
{
@@ -660,14 +662,16 @@ class CI_Form_validation {
$postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
$_in_array = TRUE;
}
+ // If we get an array field, but it's not expected - then it is most likely
+ // somebody messing with the form on the client side, so we'll just consider
+ // it an empty field
+ elseif ($allow_arrays === FALSE && is_array($this->_field_data[$row['field']]['postdata']))
+ {
+ $postdata = NULL;
+ }
else
{
- // If we get an array field, but it's not expected - then it is most likely
- // somebody messing with the form on the client side, so we'll just consider
- // it an empty field
- $postdata = is_array($this->_field_data[$row['field']]['postdata'])
- ? NULL
- : $this->_field_data[$row['field']]['postdata'];
+ $postdata = $this->_field_data[$row['field']]['postdata'];
}
// Is the rule a callback?
@@ -702,7 +706,7 @@ class CI_Form_validation {
// Ignore empty, non-required inputs with a few exceptions ...
if (
- ($postdata === NULL OR $postdata === '')
+ ($postdata === NULL OR ($allow_arrays === FALSE && $postdata === ''))
&& $callback === FALSE
&& $callable === FALSE
&& ! in_array($rule, array('required', 'isset', 'matches'), TRUE)
diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php
index ab0e1fc0a..afd070e05 100644
--- a/tests/codeigniter/libraries/Form_validation_test.php
+++ b/tests/codeigniter/libraries/Form_validation_test.php
@@ -43,7 +43,6 @@ class Form_validation_test extends CI_TestCase {
// Empty, not required
$this->assertTrue($this->run_rules($rules, array('foo' => '')));
-
// Not required, but also not empty
$this->assertTrue($this->run_rules($rules, array('foo' => '123')));
$this->assertFalse($this->run_rules($rules, array('foo' => 'bar')));
@@ -56,6 +55,13 @@ class Form_validation_test extends CI_TestCase {
$this->assertFalse($this->run_rules($rules, array('foo' => 'bar')));
}
+ public function test_rule_is_array()
+ {
+ $rules = array(array('field' => 'foo', 'label' => 'Foo', 'rules' => 'is_array'));
+ $this->assertTrue($this->run_rules($rules, array('foo' => array('1', '2'))));
+ $this->assertFalse($this->run_rules($rules, array('foo' => '')));
+ }
+
public function test_rule_matches()
{
$rules = array(
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index 2613f8677..c94ee8756 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -51,6 +51,7 @@ Release Date: Not Released
- Removed previously deprecated method ``prep_for_form()`` / rule *prep_for_form*.
- 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.
- :doc:`HTML Table Library <libraries/table>` changes include: