From c621985f69930d97beb4778bbc4d385f869dc954 Mon Sep 17 00:00:00 2001 From: euromark Date: Mon, 7 Oct 2013 23:45:27 +0200 Subject: [PATCH] Fix whitelist to be modifiable from behaviors to work with validate. --- lib/Cake/Model/ModelValidator.php | 6 ++- .../Test/Case/Model/ModelValidationTest.php | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index bbc54c8da32..70674938e40 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -249,7 +249,11 @@ public function errors($options = array()) { return $model->validationErrors; } - $fieldList = isset($options['fieldList']) ? $options['fieldList'] : array(); + $fieldList = $model->whitelist; + if (empty($fieldList) && !empty($options['fieldList'])) { + $fieldList = $options['fieldList']; + } + $exists = $model->exists(); $methods = $this->getMethods(); $fields = $this->_validationList($fieldList); diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index d313a33df12..e8114b05705 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -612,6 +612,30 @@ public function testValidatesWithAssociations() { $this->assertEquals(0, $joinRecords, 'Records were saved on the join table. %s'); } + public function testValidateWithFieldListAndBehavior() { + $TestModel = new ValidationTest1(); + $TestModel->validate = array( + 'title' => array( + 'rule' => 'alphaNumeric', + 'required' => true + ), + 'name' => array( + 'rule' => 'alphaNumeric', + 'required' => true + )); + $TestModel->Behaviors->attach('ValidationRule', array('fields' => array('name'))); + + $data = array( + 'title' => '', + 'name' => '', + ); + $result = $TestModel->save($data, array('fieldList' => array('title'))); + $this->assertFalse($result); + + $expected = array('title' => array('This field cannot be left blank'), 'name' => array('This field cannot be left blank')); + $this->assertEquals($expected, $TestModel->validationErrors); + } + /** * test that saveAll and with models with validation interact well * @@ -2380,3 +2404,21 @@ public function testValidateManyAtomicFalseDeepTrueWithErrors() { } } + +/** + * Behavior for testing validation rules. + */ +class ValidationRuleBehavior extends ModelBehavior { + + public function setup(Model $Model, $config = array()) { + $this->settings[$Model->alias] = $config; + } + + public function beforeValidate(Model $Model, $options = array()) { + $fields = $this->settings[$Model->alias]['fields']; + foreach ($fields as $field) { + $Model->whitelist[] = $field; + } + } + +} \ No newline at end of file