Browse Source

Ability to specify a custom error mesage for validatePresence and
allowEmpty

Jose Lorenzo Rodriguez 12 years ago
parent
commit
03b8db39dd
2 changed files with 66 additions and 4 deletions
  1. 40 4
      src/Validation/Validator.php
  2. 26 0
      tests/TestCase/Validation/ValidatorTest.php

+ 40 - 4
src/Validation/Validator.php

@@ -52,6 +52,23 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
 	protected $_validationDomain = 'default';
 
 /**
+ * Contains the validation messages associated to checking the presence
+ * for each corresponding field.
+ *
+ * @var array
+ */
+	protected $_presenceMessages = [];
+
+/**
+ * Contains the validation messages associated to checking the emptiness
+ * for each corresponding field.
+ *
+ * @var array
+ */
+	protected $_allowEmptyMessages= [];
+
+
+/**
  * Returns an array of fields that have failed validation. On the current model. This method will
  * actually run validation rules over data, not just return the messages.
  *
@@ -62,11 +79,17 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
  */
 	public function errors(array $data, $newRecord = true) {
 		$errors = [];
+		$requiredMessage = __d('cake', 'This field is required');
+		$emptyMessage = __d('cake', 'This field cannot be left empty');
+
 		foreach ($this->_fields as $name => $field) {
 			$keyPresent = array_key_exists($name, $data);
 
 			if (!$keyPresent && !$this->_checkPresence($field, $newRecord)) {
-				$errors[$name][] = __d('cake', 'This field is required');
+				$message = isset($this->_presenceMessages[$name])
+					? __d($this->_validationDomain, $this->_presenceMessages[$name])
+					: $requiredMessage;
+				$errors[$name][] = $message;
 				continue;
 			}
 
@@ -78,7 +101,10 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
 			$isEmpty = $this->_fieldIsEmpty($data[$name]);
 
 			if (!$canBeEmpty && $isEmpty) {
-				$errors[$name][] = __d('cake', 'This field cannot be left empty');
+				$message = isset($this->_allowEmptyMessages[$name])
+					? __d($this->_validationDomain, $this->_allowEmptyMessages[$name])
+					: $emptyMessage;
+				$errors[$name][] = $message;
 				continue;
 			}
 
@@ -293,10 +319,15 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
  *
  * @param string $field the name of the field
  * @param boolean|string $mode Valid values are true, false, 'create', 'update'
+ * @param string $message The validation message to show when if the field presence
+ * is required.
  * @return Validator this instance
  */
-	public function validatePresence($field, $mode = true) {
+	public function validatePresence($field, $mode = true, $message = null) {
 		$this->field($field)->isPresenceRequired($mode);
+		if ($message) {
+			$this->_presenceMessages[$field] = $message;
+		}
 		return $this;
 	}
 
@@ -306,10 +337,15 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
  *
  * @param string $field the name of the field
  * @param boolean|string $mode Valid values are true, false, 'create', 'update'
+ * @param string $message The validation message to show when if the field is not
+ * allowed to be empty.
  * @return Validator this instance
  */
-	public function allowEmpty($field, $mode = true) {
+	public function allowEmpty($field, $mode = true, $message = null) {
 		$this->field($field)->isEmptyAllowed($mode);
+		if ($message) {
+			$this->_allowEmptyMessages[$field] = $message;
+		}
 		return $this;
 	}
 

+ 26 - 0
tests/TestCase/Validation/ValidatorTest.php

@@ -155,6 +155,19 @@ class ValidatorTest extends \Cake\TestSuite\TestCase {
 	}
 
 /**
+ * Tests custom error messages generated when a field presence is required
+ *
+ * @return void
+ */
+	public function testCustomErrorsWithPresenceRequired() {
+		$validator = new Validator;
+		$validator->validatePresence('title', true, 'Custom message');
+		$errors = $validator->errors(['foo' => 'something']);
+		$expected = ['title' => ['Custom message']];
+		$this->assertEquals($expected, $errors);
+	}
+
+/**
  * Tests the allowEmpty method
  *
  * @return void
@@ -229,6 +242,19 @@ class ValidatorTest extends \Cake\TestSuite\TestCase {
 	}
 
 /**
+ * Tests custom error mesages generated when a field is not allowed to be empty
+ *
+ * @return void
+ */
+	public function testCustomErrorsWithEmptyNotAllowed() {
+		$validator = new Validator;
+		$validator->allowEmpty('title', false, 'Custom message');
+		$errors = $validator->errors(['title' => '']);
+		$expected = ['title' => ['Custom message']];
+		$this->assertEquals($expected, $errors);
+	}
+
+/**
  * Tests errors generated when a field is allowed to be empty
  *
  * @return void