From 63ee8f0fcdf1903851a6a90c11b15383aa042079 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 10 Nov 2009 21:20:39 -0500 Subject: [PATCH] Starting pass through validation handling. Implemented phone, and postal pass throughs. Adding tests. --- cake/libs/validation.php | 38 +++++++++++++++++++++-- cake/tests/cases/libs/validation.test.php | 34 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/cake/libs/validation.php b/cake/libs/validation.php index 643cf3d1c..8f5c59694 100644 --- a/cake/libs/validation.php +++ b/cake/libs/validation.php @@ -672,12 +672,16 @@ class Validation extends Object { if (is_null($_this->regex)) { switch ($_this->country) { case 'us': + case 'all': + case 'can': // includes all NANPA members. see http://en.wikipedia.org/wiki/North_American_Numbering_Plan#List_of_NANPA_countries_and_territories - default: $_this->regex = '/^(?:\+?1)?[-. ]?\\(?[2-9][0-8][0-9]\\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}$/'; break; } } + if (empty($_this->regex)) { + return $_this->_pass('phone', $check, $country); + } return $_this->_check(); } @@ -698,6 +702,9 @@ class Validation extends Object { if (is_array($check)) { $_this->_extract($check); } + if (empty($country)) { + $_this->country = 'us'; + } if (is_null($_this->regex)) { switch ($_this->country) { @@ -715,11 +722,13 @@ class Validation extends Object { $_this->regex = '/^[1-9]{1}[0-9]{3}$/i'; break; case 'us': - default: $_this->regex = '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i'; break; } } + if (empty($_this->regex)) { + return $_this->_pass('postal', $check, $country); + } return $_this->_check(); } @@ -834,6 +843,31 @@ class Validation extends Object { return call_user_func_array(array(&$object, $method), array($check, $args)); } +/** + * Attempts to pass unhandled Validation locales to a class starting with $classPrefix + * and ending with Validation. For example $classPrefix = 'nl', the class would be + * `NlValidation`. + * + * @param string $method The method to call on the other class. + * @param mixed $check The value to check or an array of parameters for the method to be called. + * @param string $classPrefix The prefix for the class to do the validation. + * @return mixed Return of Passed method, false on failure + * @access protected + **/ + function _pass($method, $check, $classPrefix) { + $className = ucwords($classPrefix) . 'Validation'; + if (!class_exists($className)) { + trigger_error(sprintf(__('Could not find %s class, unable to complete validation.', true), $className), E_USER_WARNING); + return false; + } + if (!method_exists($className, $method)) { + trigger_error(sprintf(__('Method %s does not exist on %s unable to complete validation.', true), $method, $className), E_USER_WARNING); + return false; + } + $check = (array)$check; + return call_user_func_array(array($className, $method), $check); + } + /** * Runs a regular expression match. * diff --git a/cake/tests/cases/libs/validation.test.php b/cake/tests/cases/libs/validation.test.php index 768c78991..aafef8988 100644 --- a/cake/tests/cases/libs/validation.test.php +++ b/cake/tests/cases/libs/validation.test.php @@ -41,6 +41,25 @@ class CustomValidator { } } +/** + * TestNlValidation class + * + * Used to test pass through of Validation + * + * @package cake.tests.cases.libs + */ +class TestNlValidation { +/** + * postal function, for testing postal pass through. + * + * @param string $check + * @return void + */ + function postal($check) { + return true; + } +} + /** * Test Case for Validation Class * @@ -1998,6 +2017,21 @@ class ValidationTest extends CakeTestCase { $this->assertTrue(Validation::postal('13089-3333')); } +/** + * test the pass through calling of an alternate locale with postal() + * + * @return void + **/ + function testPassThroughMethod() { + $this->assertTrue(Validation::postal('text', null, 'testNl')); + + $this->expectError('Could not find AUTOFAILValidation class, unable to complete validation.'); + Validation::postal('text', null, 'AUTOFAIL'); + + $this->expectError('Method phone does not exist on TestNlValidation unable to complete validation.'); + Validation::phone('text', null, 'testNl'); + } + /** * testSsn method *