Permalink
Browse files

Closes #370 & #371

Validators now properly pass format to passed functions and also now accept the format option
properly. Added 2 tests to back this up.
  • Loading branch information...
1 parent 8925dbf commit 860cffbd3d455d64dc7e31f150887a9122a5af3d @Howard3 Howard3 committed Jun 3, 2011
Showing with 29 additions and 13 deletions.
  1. +19 −1 libraries/lithium/tests/cases/util/ValidatorTest.php
  2. +10 −12 libraries/lithium/util/Validator.php
@@ -27,7 +27,6 @@ public function testCustomMethodDispatching() {
$this->assertTrue(Validator::isPhone('800-999-5555'));
$this->assertTrue(Validator::isUrl('http://google.com'));
- $this->assertTrue(Validator::isUrl('google.com', 'loose'));
}
public function testFieldOption() {
@@ -110,6 +109,25 @@ public function testUuid() {
$this->assertFalse(Validator::isUuid('1-1c0a5832-6025-11de-8a39-0800200c9a66'));
}
+ public function testCustomWithFormat() {
+ $rFormat = null;
+ $function = function(&$value, $format = null, array $options = array()) use (&$rFormat) {
+ $rFormat = $format;
+ if ($format == 'string') {
+ return true;
+ }
+ };
+ Validator::add('test', $function);
+ $validations = array(
+ 'inputName' => array( array( 'test', 'message' => 'foobar', 'format' => 'string' ) )
+ );
+ $values = array(
+ 'inputName' => 'blah'
+ );
+ $this->assertFalse((bool) Validator::check($values, $validations));
+ $this->assertEqual($rFormat, 'string');
+ }
+
/**
* Tests that new formats can be added to existing regex methods using Validator::add().
*
@@ -182,7 +182,7 @@ public static function __init() {
'visa' => '/^4\\d{12}(\\d{3})?$/',
'voyager' => '/^8699[0-9]{11}$/',
'fast' => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3' .
- '(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/',
+ '(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/'
),
'date' => array(
'dmy' => '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)' .
@@ -230,7 +230,7 @@ public static function __init() {
'right' => '/^(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))' .
'((?!\1)[,.]\d{2})?(?<!\x{00a2})\p{Sc}?$/u',
'left' => '/^(?!\x{00a2})\p{Sc}?(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?' .
- '(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?$/u',
+ '(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?$/u'
),
'notEmpty' => '/[^\s]+/m',
'phone' => '/^\+?[0-9\(\)\-]{10,20}$/',
@@ -430,7 +430,7 @@ public static function check(array $values, array $rules, array $options = array
'required' => true,
'skipEmpty' => false,
'format' => 'any',
- 'on' => null,
+ 'on' => null
);
$errors = array();
$events = (array) (isset($options['events']) ? $options['events'] : null);
@@ -604,21 +604,19 @@ protected static function _checkFormats($rules) {
$options += $defaults;
$formats = (array) $format;
- $success = false;
- if (in_array($format, array(null, 'all', 'any'))) {
- $formats = array_keys($rules);
- $options['all'] = ($format == 'all');
- }
+ $ruleIndexes = array_keys($rules);
+ $options['all'] = ($format == 'all');
- foreach ($formats as $name) {
- if (!isset($rules[$name])) {
+ foreach ($ruleIndexes as $index) {
+ if (!isset($rules[$index])) {
continue;
}
- $check = $rules[$name];
+ $check = $rules[$index];
+ $format = isset($formats[$index]) ? $formats[$index] : null;
$regexPassed = (is_string($check) && preg_match($check, $value));
- $closurePassed = (is_object($check) && $check($value, $name, $options));
+ $closurePassed = (is_object($check) && $check($value, $format, $options));
if (!$options['all'] && ($regexPassed || $closurePassed)) {
return true;

0 comments on commit 860cffb

Please sign in to comment.