Skip to content

Commit

Permalink
Removing the magic localized validation in the Validation class
Browse files Browse the repository at this point in the history
This can be done much nicer and cleaner using validation providers
with the Validator class.
  • Loading branch information
lorenzo committed Aug 31, 2014
1 parent 1eafb8e commit cef60b6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 112 deletions.
36 changes: 3 additions & 33 deletions src/Validation/Validation.php
Expand Up @@ -14,10 +14,8 @@
*/
namespace Cake\Validation;

use Cake\Core\App;
use Cake\Core\Exception\Exception;
use Cake\Utility\File;
use Cake\Utility\Number;
use RuntimeException;

/**
* Validation Class. Used for validation of model data
Expand Down Expand Up @@ -670,9 +668,7 @@ public static function phone($check, $regex = null, $country = 'all') {
break;
}
}
if (empty($regex)) {
return static::_pass('phone', $check, $country);
}

return static::_check($check, $regex);
}

Expand Down Expand Up @@ -711,9 +707,7 @@ public static function postal($check, $regex = null, $country = 'us') {
break;
}
}
if (empty($regex)) {
return static::_pass('postal', $check, $country);
}

return static::_check($check, $regex);
}

Expand Down Expand Up @@ -809,30 +803,6 @@ public static function uuid($check) {
return self::_check($check, $regex);
}

/**
* 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
*/
protected static function _pass($method, $check, $classPrefix) {
$className = App::className($classPrefix, 'Validation', 'Validation');
if (!$className) {
trigger_error('Could not find class for validation, unable to complete validation.', E_USER_WARNING);
return false;
}
if (!method_exists($className, $method)) {
trigger_error(sprintf('Method %s does not exist on %s unable to complete validation.', $method, $className), E_USER_WARNING);
return false;
}
$check = (array)$check;
return call_user_func_array(array($className, $method), $check);
}

/**
* Runs a regular expression match.
*
Expand Down
79 changes: 0 additions & 79 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -39,46 +39,6 @@ public static function customValidate($check) {

}

/**
* TestNlValidation class
*
* Used to test pass through of Validation
*
*/
class TestNlValidation {

/**
* postal function, for testing postal pass through.
*
* @param string $check
* @return void
*/
public static function postal($check) {
return true;
}

}

/**
* TestDeValidation class
*
* Used to test pass through of Validation
*
*/
class TestDeValidation {

/**
* phone function, for testing phone pass through.
*
* @param string $check
* @return void
*/
public static function phone($check) {
return true;
}

}

/**
* Test Case for Validation Class
*
Expand Down Expand Up @@ -2275,45 +2235,6 @@ public function testPostal() {
$this->assertTrue(Validation::postal('13089-3333'));
}

/**
* Test that phone and postal pass to other classes.
*
* @return void
*/
public function testPostalPhonePass() {
$this->assertTrue(Validation::postal('text', null, __NAMESPACE__ . '\TestNlValidation'));
$this->assertTrue(Validation::phone('text', null, __NAMESPACE__ . '\TestDeValidation'));
}

/**
* test pass through failure on postal
*
* @expectedException PHPUnit_Framework_Error
* @return void
*/
public function testPassThroughMethodFailure() {
Validation::phone('text', null, __NAMESPACE__ . '\TestNlValidation');
}

/**
* test the pass through calling of an alternate locale with postal()
*
* @expectedException PHPUnit_Framework_Error
* @return void
*/
public function testPassThroughClassFailure() {
Validation::postal('text', null, 'AUTOFAIL');
}

/**
* test pass through method
*
* @return void
*/
public function testPassThroughMethod() {
$this->assertTrue(Validation::postal('text', null, __NAMESPACE__ . '\TestNlValidation'));
}

/**
* testUserDefined method
*
Expand Down

0 comments on commit cef60b6

Please sign in to comment.