From 4f3b2664910104ef86589c75f7158aa5d5cc39eb Mon Sep 17 00:00:00 2001 From: Thomas Ploch Date: Tue, 7 Feb 2012 12:15:04 +0100 Subject: [PATCH] Added support for simplified required validation on 'create' or 'update'. Added testcases for new 'required' attributes and conflicts with the 'on' key. Fixes #230. --- lib/Cake/Model/Model.php | 7 +- .../Test/Case/Model/ModelValidationTest.php | 177 ++++++++++++++++++ 2 files changed, 182 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 8986405f038..4a2a6900714 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -3016,13 +3016,16 @@ public function invalidFields($options = array()) { } $validator = array_merge($default, $validator); - if (!empty($validator['on'])) { + if (!empty($validator['on']) || in_array($validator['required'], array('create', 'update'))) { if ($exists === null) { $exists = $this->exists(); } - if (($validator['on'] == 'create' && $exists) || ($validator['on'] == 'update' && !$exists)) { + if ($validator['on'] == 'create' && $exists || $validator['on'] == 'update' && !$exists) { continue; } + if ($validator['required'] == 'create' && !$exists || $validator['required'] == 'update' && $exists) { + $validator['required'] = true; + } } $valid = true; diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index 8435eda3a77..692fe987611 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -814,4 +814,181 @@ function testStateValidation() { $this->assertFalse($Article->validates()); } +/** + * Test for 'required' => [create|update] in validation rules. + * + * @return void + */ + function testStateRequiredValidation() { + $this->loadFixtures('Article'); + $Article = new Article(); + + // no title field present + $data = array( + 'Article' => array( + 'body' => 'Extra Fields Body', + 'published' => '1' + ) + ); + + $Article->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => 'create' + ) + ) + ); + + $Article->create($data); + $this->assertFalse($Article->validates()); + + $Article->save(null, array('validate' => false)); + $data['Article']['id'] = $Article->id; + $Article->set($data); + $this->assertTrue($Article->validates()); + + unset($data['Article']['id']); + $Article->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => 'update' + ) + ) + ); + + $Article->create($data); + $this->assertTrue($Article->validates()); + + $Article->save(null, array('validate' => false)); + $data['Article']['id'] = $Article->id; + $Article->set($data); + $this->assertFalse($Article->validates()); + } + +/** + * Test that 'required' and 'on' are not conflicting + * + * @return void + */ + function testOnRequiredConflictValidation() { + $this->loadFixtures('Article'); + $Article = new Article(); + + // no title field present + $data = array( + 'Article' => array( + 'body' => 'Extra Fields Body', + 'published' => '1' + ) + ); + + $Article->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => 'create', + 'on' => 'create' + ) + ) + ); + + $Article->create($data); + $this->assertFalse($Article->validates()); + + $Article->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => 'update', + 'on' => 'create' + ) + ) + ); + + $Article->create($data); + $this->assertTrue($Article->validates()); + + $Article->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => 'create', + 'on' => 'update' + ) + ) + ); + + $Article->create($data); + $this->assertTrue($Article->validates()); + + $Article->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => 'update', + 'on' => 'update' + ) + ) + ); + + $Article->create($data); + $this->assertTrue($Article->validates()); + + $Article->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => 'create', + 'on' => 'create' + ) + ) + ); + + $Article->save(null, array('validate' => false)); + $data['Article']['id'] = $Article->id; + $Article->set($data); + $this->assertTrue($Article->validates()); + + $Article->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => 'update', + 'on' => 'create' + ) + ) + ); + + $Article->set($data); + $this->assertTrue($Article->validates()); + + $Article->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => 'create', + 'on' => 'update' + ) + ) + ); + + $Article->set($data); + $this->assertTrue($Article->validates()); + + $Article->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => 'update', + 'on' => 'update' + ) + ) + ); + + $Article->set($data); + $this->assertFalse($Article->validates()); + } + }