Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch '1.3-validation' into 1.3-misc

  • Loading branch information...
commit fa14f9cebcdff88df5af2378c73c85e54ceabdb2 2 parents b452e83 + 47e6f33
@markstory markstory authored
Showing with 86 additions and 15 deletions.
  1. +52 −15 cake/libs/validation.php
  2. +34 −0 cake/tests/cases/libs/validation.test.php
View
67 cake/libs/validation.php
@@ -213,8 +213,8 @@ function blank($check) {
*
* @param mixed $check credit card number to validate
* @param mixed $type 'all' may be passed as a sting, defaults to fast which checks format of most major credit cards
- * if an array is used only the values of the array are checked.
- * Example: array('amex', 'bankcard', 'maestro')
+ * if an array is used only the values of the array are checked.
+ * Example: array('amex', 'bankcard', 'maestro')
* @param boolean $deep set to true this will check the Luhn algorithm of the credit card.
* @param string $regex A custom regex can also be passed, this will be used instead of the defined regex values
* @return boolean Success
@@ -608,12 +608,14 @@ function money($check, $symbolPosition = 'left') {
/**
* Validate a multiple select.
*
+ * Valid Options
+ *
+ * - in => provide a list of choices that selections must be made from
+ * - max => maximun number of non-zero choices that can be made
+ * - min => minimum number of non-zero choices that can be made
+ *
* @param mixed $check Value to check
* @param mixed $options Options for the check.
- * Valid options
- * in => provide a list of choices that selections must be made from
- * max => maximun number of non-zero choices that can be made
- * min => minimum number of non-zero choices that can be made
* @return boolean Success
* @access public
*/
@@ -672,12 +674,16 @@ function phone($check, $regex = null, $country = 'all') {
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 +704,9 @@ function postal($check, $regex = null, $country = null) {
if (is_array($check)) {
$_this->_extract($check);
}
+ if (empty($country)) {
+ $_this->country = 'us';
+ }
if (is_null($_this->regex)) {
switch ($_this->country) {
@@ -715,11 +724,13 @@ function postal($check, $regex = null, $country = null) {
$_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();
}
@@ -783,13 +794,14 @@ function ssn($check, $regex = null, $country = null) {
* Checks that a value is a valid URL according to http://www.w3.org/Addressing/URL/url-spec.txt
*
* The regex checks for the following component parts:
- * a valid, optional, scheme
- * a valid ip address OR
- * a valid domain name as defined by section 2.3.1 of http://www.ietf.org/rfc/rfc1035.txt
- * with an optional port number
- * an optional valid path
- * an optional query string (get parameters)
- * an optional fragment (anchor tag)
+ *
+ * - a valid, optional, scheme
+ * - a valid ip address OR
+ * a valid domain name as defined by section 2.3.1 of http://www.ietf.org/rfc/rfc1035.txt
+ * with an optional port number
+ * - an optional valid path
+ * - an optional query string (get parameters)
+ * - an optional fragment (anchor tag)
*
* @param string $check Value to check
* @param boolean $strict Require URL to be prefixed by a valid scheme (one of http(s)/ftp(s)/file/news/gopher)
@@ -835,6 +847,31 @@ function userDefined($check, $object, $method, $args = null) {
}
/**
+ * 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.
*
* @return boolean Success of match
View
34 cake/tests/cases/libs/validation.test.php
@@ -42,6 +42,25 @@ function customValidate($check) {
}
/**
+ * 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
*
* @package cake
@@ -1999,6 +2018,21 @@ function testPostal() {
}
/**
+ * 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
*
* @access public
Please sign in to comment.
Something went wrong with that request. Please try again.