Browse Source

Add mode to notEmpty() and remove message from allowEmpty().

Having `$message` for allowEmpty() doesn't make sense as it will only
remove validation errors now, not cause them to be triggered. Add
`$mode` to notEmpty() as it is very relevant there.
mark_story 12 years ago
parent
commit
d772b7d8d5
2 changed files with 19 additions and 13 deletions
  1. 14 9
      src/Validation/Validator.php
  2. 5 4
      tests/TestCase/Validation/ValidatorTest.php

+ 14 - 9
src/Validation/Validator.php

@@ -312,25 +312,30 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
  * Allows a field to be empty.
  *
  * This is the opposite of notEmpty() which requires a field to not be empty.
+ * By using $mode equal to 'create' or 'update', you can allow fields to be empty
+ * when records are first created, or when they are updated.
+ *
+ * Because this and `notEmpty()` modify the same internal state, the last
+ * method called will take precedence.
  *
  * @param string $field the name of the field
- * @param string $message The validation message to show if the field is not
  * @param bool|string $mode Valid values are true, 'create', 'update'
- * allowed to be empty.
  * @return Validator this instance
  */
-	public function allowEmpty($field, $message = null, $mode = true) {
+	public function allowEmpty($field, $mode = true) {
 		$this->field($field)->isEmptyAllowed($mode);
-		if ($message) {
-			$this->_allowEmptyMessages[$field] = $message;
-		}
 		return $this;
 	}
 
 /**
- * Sets a field to be required as notEmpty.
+ * Sets a field to require a non-empty value.
  *
  * This is the opposite of allowEmpty() which allows a field to be empty.
+ * By using $mode equal to 'create' or 'update', you can make fields required
+ * when records are first created, or when they are updated.
+ *
+ * Because this and `allowEmpty()` modify the same internal state, the last
+ * method called will take precedence.
  *
  * @param string $field the name of the field
  * @param string $message The validation message to show if the field is not
@@ -338,8 +343,8 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
  * allowed to be empty.
  * @return Validator this instance
  */
-	public function notEmpty($field, $message = null) {
-		$this->field($field)->isEmptyAllowed(false);
+	public function notEmpty($field, $message = null, $mode = false) {
+		$this->field($field)->isEmptyAllowed($mode);
 		if ($message) {
 			$this->_allowEmptyMessages[$field] = $message;
 		}

+ 5 - 4
tests/TestCase/Validation/ValidatorTest.php

@@ -177,10 +177,10 @@ class ValidatorTest extends \Cake\TestSuite\TestCase {
 		$this->assertSame($validator, $validator->allowEmpty('title'));
 		$this->assertTrue($validator->field('title')->isEmptyAllowed());
 
-		$validator->allowEmpty('title', null, 'created');
+		$validator->allowEmpty('title', 'created');
 		$this->assertEquals('created', $validator->field('title')->isEmptyAllowed());
 
-		$validator->allowEmpty('title', null, 'updated');
+		$validator->allowEmpty('title', 'updated');
 		$this->assertEquals('updated', $validator->field('title')->isEmptyAllowed());
 	}
 
@@ -190,6 +190,7 @@ class ValidatorTest extends \Cake\TestSuite\TestCase {
  * @return void
  */
 	public function testNotEmpty() {
+		$validator = new Validator;
 		$validator->notEmpty('title');
 		$this->assertFalse($validator->field('title')->isEmptyAllowed());
 
@@ -212,11 +213,11 @@ class ValidatorTest extends \Cake\TestSuite\TestCase {
 		$this->assertFalse($validator->isEmptyAllowed('title', true));
 		$this->assertFalse($validator->isEmptyAllowed('title', false));
 
-		$validator->allowEmpty('title', null, 'create');
+		$validator->allowEmpty('title', 'create');
 		$this->assertTrue($validator->isEmptyAllowed('title', true));
 		$this->assertFalse($validator->isEmptyAllowed('title', false));
 
-		$validator->allowEmpty('title', null, 'update');
+		$validator->allowEmpty('title', 'update');
 		$this->assertTrue($validator->isEmptyAllowed('title', false));
 		$this->assertFalse($validator->isEmptyAllowed('title', true));
 	}