Merge branch 'master' of github.com:cakephp/cakephp

This commit is contained in:
mark_story 2012-08-25 17:53:22 -04:00
commit 7966c00700
7 changed files with 77 additions and 11 deletions

View file

@ -63,6 +63,20 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
*/
protected $_methods = array();
/**
* Holds the available custom callback methods from the model
*
* @var array
*/
protected $_modelMethods = array();
/**
* Holds the list of behavior names that were attached when this object was created
*
* @var array
*/
protected $_behaviors = array();
/**
* Constructor
*
@ -280,15 +294,19 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* @return array List of callables to be used as validation methods
*/
public function getMethods() {
if (!empty($this->_methods)) {
$behaviors = $this->_model->Behaviors->enabled();
if (!empty($this->_methods) && $behaviors === $this->_behaviors) {
return $this->_methods;
}
$this->_behaviors = $behaviors;
$methods = array();
foreach (get_class_methods($this->_model) as $method) {
$methods[strtolower($method)] = array($this->_model, $method);
if (empty($this->_modelMethods)) {
foreach (get_class_methods($this->_model) as $method) {
$this->_modelMethods[strtolower($method)] = array($this->_model, $method);
}
}
$methods = $this->_modelMethods;
foreach (array_keys($this->_model->Behaviors->methods()) as $method) {
$methods += array(strtolower($method) => array($this->_model, $method));
}

View file

@ -282,6 +282,17 @@ class CakeValidationRule {
return true;
}
/**
* Resets interal state for this rule, by default it will become valid
* and it will set isUpdate() to false
*
* @return void
**/
public function reset() {
$this->_valid = true;
$this->_recordExists = false;
}
/**
* Returns passed options for this rule
*

View file

@ -117,6 +117,7 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
* @return array list of validation errors for this field
*/
public function validate($data, $isUpdate = false) {
$this->reset();
$errors = array();
foreach ($this->getRules() as $name => $rule) {
$rule->isUpdate($isUpdate);
@ -143,6 +144,17 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
return $errors;
}
/**
* Resets interal state for all validation rules in this set
*
* @return void
**/
public function reset() {
foreach ($this->getRules() as $rule) {
$rule->reset();
}
}
/**
* Gets a rule for a given name if exists
*

View file

@ -1700,6 +1700,35 @@ class ModelValidationTest extends BaseModelTest {
$this->assertEquals($expected, array_keys($result));
}
/**
* Tests that methods are refreshed when the list of behaviors change
*
* @return void
*/
public function testGetMethodsRefresh() {
$this->loadFixtures('Article', 'Comment');
$TestModel = new Article();
$Validator = $TestModel->validator();
$result = $Validator->getMethods();
$expected = array_map('strtolower', get_class_methods('Article'));
$this->assertEquals($expected, array_keys($result));
$TestModel->Behaviors->attach('Containable');
$newList = array(
'contain',
'resetbindings',
'containments',
'fielddependencies',
'containmentsmap'
);
$this->assertEquals(array_merge($expected, $newList), array_keys($Validator->getMethods()));
$TestModel->Behaviors->detach('Containable');
$this->assertEquals($expected, array_keys($Validator->getMethods()));
}
/**
* testSetValidationDomain method
*

View file

@ -3242,7 +3242,6 @@ class ModelWriteTest extends BaseModelTest {
)
),
1 => array(
'body' => array('This field cannot be left blank'),
'Comment' => array(
0 => array(
'User' => array(
@ -3687,9 +3686,6 @@ class ModelWriteTest extends BaseModelTest {
$expected = array(
0 => array(
'body' => array('This field cannot be left blank')
),
1 => array(
'body' => array('This field cannot be left blank')
)
);
$result = $TestModel->validationErrors;
@ -3703,7 +3699,7 @@ class ModelWriteTest extends BaseModelTest {
)
),
array(
'Article' => array('id' => 2, 'body' => 'Same here'),
'Article' => array('id' => 2),
'Comment' => array(
array('comment' => '', 'published' => 'Y', 'user_id' => 2)
)

View file

@ -276,7 +276,7 @@ class Article extends CakeTestModel {
public $validate = array(
'user_id' => 'numeric',
'title' => array('required' => false, 'rule' => 'notEmpty'),
'body' => 'notEmpty',
'body' => array('required' => false, 'rule' => 'notEmpty'),
);
/**

View file

@ -919,7 +919,7 @@ class View extends Object {
include $this->__viewFile;
unset($this->_viewFile);
unset($this->__viewFile);
return ob_get_clean();
}