Skip to content

Commit 40499d4

Browse files
author
thinkingmedia
committed
adds validation for arrays
1 parent fd66542 commit 40499d4

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/Validation/Validation.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,17 @@ public static function isInteger($value)
11811181
return (bool)preg_match('/^-?[0-9]+$/', $value);
11821182
}
11831183

1184+
/**
1185+
* Check that the input value is an array.
1186+
*
1187+
* @param array $value
1188+
* @return bool
1189+
*/
1190+
public static function isArray($value)
1191+
{
1192+
return is_array($value);
1193+
}
1194+
11841195
/**
11851196
* Converts an array representing a date or datetime into a ISO string.
11861197
* The arrays are typically sent for validation from a form generated by

src/Validation/Validator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,23 @@ public function integer($field, $message = null, $when = null)
13011301
]);
13021302
}
13031303

1304+
/**
1305+
* Add a validation rule to ensure that a field contains an array.
1306+
*
1307+
* @param string $field The field you want to apply the rule to.
1308+
* @param string|null $message The error message when the rule fails.
1309+
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
1310+
* true when the validation rule should be applied.
1311+
* @return $this
1312+
*/
1313+
public function isArray($field, $message = null, $when = null)
1314+
{
1315+
$extra = array_filter(['on' => $when, 'message' => $message]);
1316+
return $this->add($field, 'isArray', $extra + [
1317+
'rule' => 'isArray'
1318+
]);
1319+
}
1320+
13041321
/**
13051322
* Add a validation rule for a multiple select. Comparison is case sensitive by default.
13061323
*

tests/TestCase/Validation/ValidationTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
*/
1515
namespace Cake\Test\TestCase\Validation;
1616

17+
use Cake\Collection\Collection;
1718
use Cake\Core\Configure;
1819
use Cake\Filesystem\File;
1920
use Cake\I18n\I18n;
2021
use Cake\TestSuite\TestCase;
2122
use Cake\Validation\Validation;
23+
use Cake\Validation\Validator;
2224
use Locale;
2325

2426
require_once __DIR__ . '/stubs.php';
@@ -2667,6 +2669,21 @@ public function testLongitude()
26672669
$this->assertFalse(Validation::longitude('-190.52236'));
26682670
}
26692671

2672+
/**
2673+
* Test isArray
2674+
*
2675+
* @return void
2676+
*/
2677+
public function testIsArray()
2678+
{
2679+
$this->assertTrue(Validation::isArray([]));
2680+
$this->assertTrue(Validation::isArray([1,2,3]));
2681+
$this->assertTrue(Validation::isArray(['key'=>'value']));
2682+
$this->assertFalse(Validation::isArray('[1,2,3]'));
2683+
$this->assertFalse(Validation::isArray(new Collection([])));
2684+
$this->assertFalse(Validation::isArray(10));
2685+
}
2686+
26702687
/**
26712688
* Test isInteger
26722689
*

0 commit comments

Comments
 (0)