Permalink
Browse files

Added support for simplified required validation on 'create' or 'upda…

…te'. Added testcases for new 'required' attributes and conflicts with the 'on' key. Fixes #230.
  • Loading branch information...
1 parent b6f99bc commit 4f3b2664910104ef86589c75f7158aa5d5cc39eb Thomas Ploch committed Feb 7, 2012
Showing with 182 additions and 2 deletions.
  1. +5 −2 lib/Cake/Model/Model.php
  2. +177 −0 lib/Cake/Test/Case/Model/ModelValidationTest.php
View
@@ -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;
@@ -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());
+ }
+
}

0 comments on commit 4f3b266

Please sign in to comment.