Browse Source

Tweak Validator API to be more user friendly.

Using `allowEmpty('field', false);` doesn't read as well as
`notEmpty('field)`. I feel the mode will be less frequently used over
setting a custom message, so switch the order of those parameters
around.
mark_story 12 years ago
parent
commit
abddb826b7
2 changed files with 44 additions and 14 deletions
  1. 24 4
      src/Validation/Validator.php
  2. 20 10
      tests/TestCase/Validation/ValidatorTest.php

+ 24 - 4
src/Validation/Validator.php

@@ -309,16 +309,17 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
 	}
 
 /**
- * Sets whether a field is allowed to be empty. If it is,  all other validation
- * rules will be ignored
+ * Allows a field to be empty.
+ *
+ * This is the opposite of notEmpty() which requires a field to not be empty.
  *
  * @param string $field the name of the field
- * @param bool|string $mode Valid values are true, false, 'create', 'update'
  * @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, $mode = true, $message = null) {
+	public function allowEmpty($field, $message = null, $mode = true) {
 		$this->field($field)->isEmptyAllowed($mode);
 		if ($message) {
 			$this->_allowEmptyMessages[$field] = $message;
@@ -327,6 +328,25 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
 	}
 
 /**
+ * Sets a field to be required as notEmpty.
+ *
+ * This is the opposite of allowEmpty() which allows a field to be empty.
+ *
+ * @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 false, 'create', 'update'
+ * allowed to be empty.
+ * @return Validator this instance
+ */
+	public function notEmpty($field, $message = null) {
+		$this->field($field)->isEmptyAllowed(false);
+		if ($message) {
+			$this->_allowEmptyMessages[$field] = $message;
+		}
+		return $this;
+	}
+
+/**
  * Returns whether or not a field can be left empty for a new or already existing
  * record.
  *

+ 20 - 10
tests/TestCase/Validation/ValidatorTest.php

@@ -177,17 +177,27 @@ class ValidatorTest extends \Cake\TestSuite\TestCase {
 		$this->assertSame($validator, $validator->allowEmpty('title'));
 		$this->assertTrue($validator->field('title')->isEmptyAllowed());
 
-		$validator->allowEmpty('title', false);
-		$this->assertFalse($validator->field('title')->isEmptyAllowed());
-
-		$validator->allowEmpty('title', 'created');
+		$validator->allowEmpty('title', null, 'created');
 		$this->assertEquals('created', $validator->field('title')->isEmptyAllowed());
 
-		$validator->allowEmpty('title', 'updated');
+		$validator->allowEmpty('title', null, 'updated');
 		$this->assertEquals('updated', $validator->field('title')->isEmptyAllowed());
 	}
 
 /**
+ * Test the notEmpty() method.
+ *
+ * @return void
+ */
+	public function testNotEmpty() {
+		$validator->notEmpty('title');
+		$this->assertFalse($validator->field('title')->isEmptyAllowed());
+
+		$validator->allowEmpty('title');
+		$this->assertTrue($validator->field('title')->isEmptyAllowed());
+	}
+
+/**
  * Tests the isEmptyAllowed method
  *
  * @return void
@@ -198,15 +208,15 @@ class ValidatorTest extends \Cake\TestSuite\TestCase {
 		$this->assertTrue($validator->isEmptyAllowed('title', true));
 		$this->assertTrue($validator->isEmptyAllowed('title', false));
 
-		$validator->allowEmpty('title', false);
+		$validator->notEmpty('title');
 		$this->assertFalse($validator->isEmptyAllowed('title', true));
 		$this->assertFalse($validator->isEmptyAllowed('title', false));
 
-		$validator->allowEmpty('title', 'create');
+		$validator->allowEmpty('title', null, 'create');
 		$this->assertTrue($validator->isEmptyAllowed('title', true));
 		$this->assertFalse($validator->isEmptyAllowed('title', false));
 
-		$validator->allowEmpty('title', 'update');
+		$validator->allowEmpty('title', null, 'update');
 		$this->assertTrue($validator->isEmptyAllowed('title', false));
 		$this->assertFalse($validator->isEmptyAllowed('title', true));
 	}
@@ -218,7 +228,7 @@ class ValidatorTest extends \Cake\TestSuite\TestCase {
  */
 	public function testErrorsWithEmptyNotAllowed() {
 		$validator = new Validator;
-		$validator->allowEmpty('title', false);
+		$validator->notEmpty('title');
 		$errors = $validator->errors(['title' => '']);
 		$expected = ['title' => ['This field cannot be left empty']];
 		$this->assertEquals($expected, $errors);
@@ -248,7 +258,7 @@ class ValidatorTest extends \Cake\TestSuite\TestCase {
  */
 	public function testCustomErrorsWithEmptyNotAllowed() {
 		$validator = new Validator;
-		$validator->allowEmpty('title', false, 'Custom message');
+		$validator->notEmpty('title', 'Custom message');
 		$errors = $validator->errors(['title' => '']);
 		$expected = ['title' => ['Custom message']];
 		$this->assertEquals($expected, $errors);