Skip to content

Commit

Permalink
Ability to specify a custom error mesage for validatePresence and
Browse files Browse the repository at this point in the history
allowEmpty
  • Loading branch information
lorenzo committed Mar 14, 2014
1 parent 6184831 commit 03b8db3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/Validation/Validator.php
Expand Up @@ -51,6 +51,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.
Expand All @@ -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;
}

Expand All @@ -78,7 +101,10 @@ public function errors(array $data, $newRecord = true) {
$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;
}

Expand Down Expand Up @@ -293,10 +319,15 @@ public function remove($field, $rule = null) {
*
* @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;
}

Expand All @@ -306,10 +337,15 @@ public function validatePresence($field, $mode = true) {
*
* @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;
}

Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/Validation/ValidatorTest.php
Expand Up @@ -154,6 +154,19 @@ public function testErrorsWithPresenceRequired() {
$this->assertEmpty($validator->errors(['foo' => 'bar']));
}

/**
* 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
*
Expand Down Expand Up @@ -228,6 +241,19 @@ public function testErrorsWithEmptyNotAllowed() {
$this->assertEmpty($errors);
}

/**
* 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
*
Expand Down

0 comments on commit 03b8db3

Please sign in to comment.