diff --git a/Cake/ORM/Table.php b/Cake/ORM/Table.php index 537abed27c0..28ba596c76e 100644 --- a/Cake/ORM/Table.php +++ b/Cake/ORM/Table.php @@ -27,6 +27,7 @@ use Cake\ORM\Association\HasOne; use Cake\ORM\BehaviorRegistry; use Cake\ORM\Entity; +use Cake\ORM\Validator; use Cake\Utility\Inflector; /** @@ -788,8 +789,8 @@ public function validator($name = 'default', $instance = null) { return $this->_validators[$name] = $instance; } - $validator = new Validator; - $validator = $this->{'validation' . $name}($validator); + $validator = new Validator($this); + $validator = $this->{'validation' . ucfirst($name)}($validator); return $this->_validators[$name] = $validator; } diff --git a/Cake/ORM/Validator.php b/Cake/ORM/Validator.php index ee36ddd57f7..06cdfebb609 100644 --- a/Cake/ORM/Validator.php +++ b/Cake/ORM/Validator.php @@ -14,7 +14,7 @@ * @since CakePHP(tm) v 2.2.0 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ -namespace Cake\Model; +namespace Cake\ORM; use Cake\ORM\Validation\ValidationSet; use Cake\Utility\Hash; @@ -27,7 +27,7 @@ * * @link http://book.cakephp.org/2.0/en/data-validation.html */ -class ModelValidator implements \ArrayAccess, \IteratorAggregate, \Countable { +class Validator implements \ArrayAccess, \IteratorAggregate, \Countable { /** * Holds the ValidationSet objects array @@ -79,7 +79,7 @@ class ModelValidator implements \ArrayAccess, \IteratorAggregate, \Countable { * @param \Cake\ORM\Table $table A reference to the table object this is bound to */ public function __construct(Table $table) { - $this->_model = $model; + $this->_table = $table; } /** diff --git a/Cake/Test/TestCase/ORM/TableTest.php b/Cake/Test/TestCase/ORM/TableTest.php index c3fc4317b95..2bffb379932 100644 --- a/Cake/Test/TestCase/ORM/TableTest.php +++ b/Cake/Test/TestCase/ORM/TableTest.php @@ -1797,4 +1797,43 @@ public function testHasField() { $this->assertTrue($table->hasField('body'), 'Should be there.'); } +/** + * Tests that there exists a default validator + * + * @return void + */ + public function testValidatorDefault() { + $table = new Table(); + $validator = $table->validator(); + $this->assertInstanceOf('\Cake\ORM\Validator', $validator); + $default = $table->validator('default'); + $this->assertSame($validator, $default); + } + +/** + * Tests that it is possible to define custom validator methods + * + * @return void + */ + public function functionTestValidationWithDefiner() { + $table = $this->getMock('\Cake\ORM\Table', ['validationForOtherStuff']); + $table->expects($this->once())->method('validationForOtherStuff') + ->will($this->returnArgument(0)); + $other = $table->validator('forOtherStuff'); + $this->assertInstanceOf('\Cake\ORM\Validator', $other); + $this->assertNotSame($other, $table->validator()); + } + +/** + * Tests that it is possible to set a custom validator under a name + * + * @return void + */ + public function testValidatorSetter() { + $table = new Table; + $validator = new \Cake\ORM\Validator($table); + $table->validator('other', $validator); + $this->assertSame($validator, $table->validator('other')); + } + }