Permalink
Browse files

Revert default value of allowEmpty.

In 2.1, the default value was null, which does not impart any behavior.
In 2.2 this was changed to false, which makes it hard to validate ''
with validation methods.  Move some tests around and update tests that
probably should have been failing before.

Fixes #2983
  • Loading branch information...
1 parent b2ccdd4 commit eb7b66b37c4bb9535204c93e69569cf766ae0d28 @markstory markstory committed Jun 25, 2012
View
2 lib/Cake/Model/Validator/CakeValidationRule.php
@@ -85,7 +85,7 @@ class CakeValidationRule {
*
* @var boolean
*/
- public $allowEmpty = false;
+ public $allowEmpty = null;
/**
* The 'on' key
View
20 lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -2178,4 +2178,24 @@ public function testCustomMethodsWithCakeValidationSet() {
$this->assertEquals($expected, $result);
}
+ public function testCustomMethodWithEmptyValue() {
+ $this->loadFixtures('Article');
+
+ $model = $this->getMock('Article', array('isLegit'));
+ $model->validate = array(
+ 'title' => array(
+ 'custom' => array(
+ 'rule' => array('isLegit'),
+ 'message' => 'is no good'
+ )
+ )
+ );
+ $model->expects($this->once())
+ ->method('isLegit')
+ ->will($this->returnValue(false));
+
+ $model->set(array('title' => ''));
+ $this->assertFalse($model->validates());
+ }
+
}
View
81 lib/Cake/Test/Case/Model/ModelWriteTest.php
@@ -3002,23 +3002,6 @@ public function testSaveAllAtomic() {
), array('atomic' => false));
$this->assertSame($result, array(true, true, true));
- $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
- $result = $TestModel->saveAll(array(
- array(
- 'id' => '1',
- 'title' => 'Un-Baleeted First Post',
- 'body' => 'Not Baleeted!',
- 'published' => 'Y'
- ),
- array(
- 'id' => '2',
- 'title' => '',
- 'body' => 'Trying to get away with an empty title'
- )
- ), array('validate' => true, 'atomic' => false));
-
- $this->assertSame(array(true, false), $result);
-
$result = $TestModel->saveAll(array(
'Article' => array('id' => 2),
'Comment' => array(
@@ -3034,6 +3017,25 @@ public function testSaveAllAtomic() {
))
), array('validate' => true, 'atomic' => false));
$this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
+
+ $TestModel->validate = array(
+ 'title' => 'notEmpty',
+ 'author_id' => 'numeric'
+ );
+ $result = $TestModel->saveAll(array(
+ array(
+ 'id' => '1',
+ 'title' => 'Un-Baleeted First Post',
+ 'body' => 'Not Baleeted!',
+ 'published' => 'Y'
+ ),
+ array(
+ 'id' => '2',
+ 'title' => '',
+ 'body' => 'Trying to get away with an empty title'
+ )
+ ), array('validate' => true, 'atomic' => false));
+ $this->assertSame(array(true, false), $result);
}
/**
@@ -3189,6 +3191,25 @@ public function testSaveAllDeepMany() {
$data = array(
array(
+ 'Article' => array('id' => 1),
+ 'Comment' => array(
+ array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
+ array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
+ )
+ ),
+ array(
+ 'Article' => array('id' => 2),
+ 'Comment' => array(
+ array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
+ array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
+ )
+ )
+ );
+ $result = $TestModel->saveAll($data, array('deep' => true));
+ $this->assertTrue($result);
+
+ $data = array(
+ array(
'id' => 1, 'body' => '',
'Comment' => array(
array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
@@ -3220,6 +3241,7 @@ public function testSaveAllDeepMany() {
)
),
1 => array(
+ 'body' => array('This field cannot be left blank'),
'Comment' => array(
0 => array(
'User' => array(
@@ -3234,25 +3256,6 @@ public function testSaveAllDeepMany() {
);
$result = $TestModel->validationErrors;
$this->assertSame($expected, $result);
-
- $data = array(
- array(
- 'Article' => array('id' => 1),
- 'Comment' => array(
- array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
- array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
- )
- ),
- array(
- 'Article' => array('id' => 2),
- 'Comment' => array(
- array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
- array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
- )
- )
- );
- $result = $TestModel->saveAll($data, array('deep' => true));
- $this->assertTrue($result);
}
/**
* testSaveAllDeepValidateOnly
@@ -3661,7 +3664,8 @@ public function testSaveAllNotDeepMany() {
$data = array(
array(
- 'id' => 1, 'body' => '',
+ 'id' => 1,
+ 'body' => '',
'Comment' => array(
array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
@@ -3682,6 +3686,9 @@ public function testSaveAllNotDeepMany() {
$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;
View
2 lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php
@@ -129,7 +129,7 @@ public function testIsRequired() {
*
* @return void
*/
- public function testIsEmplyAllowed() {
+ public function testIsEmptyAllowed() {
$def = array('rule' => 'aRule', 'allowEmpty' => true);
$Rule = new CakeValidationRule($def);
$this->assertTrue($Rule->isEmptyAllowed());
View
6 lib/Cake/Test/Case/Model/models.php
@@ -273,7 +273,11 @@ class Article extends CakeTestModel {
*
* @var array
*/
- public $validate = array('user_id' => 'numeric', 'title' => array('allowEmpty' => false, 'rule' => 'notEmpty'), 'body' => 'notEmpty');
+ public $validate = array(
+ 'user_id' => 'numeric',
+ 'title' => array('required' => false, 'rule' => 'notEmpty'),
+ 'body' => 'notEmpty',
+ );
/**
* beforeSaveReturn property

0 comments on commit eb7b66b

Please sign in to comment.