Skip to content

Commit 03b8db3

Browse files
committed
Ability to specify a custom error mesage for validatePresence and
allowEmpty
1 parent 6184831 commit 03b8db3

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

src/Validation/Validator.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
5151
*/
5252
protected $_validationDomain = 'default';
5353

54+
/**
55+
* Contains the validation messages associated to checking the presence
56+
* for each corresponding field.
57+
*
58+
* @var array
59+
*/
60+
protected $_presenceMessages = [];
61+
62+
/**
63+
* Contains the validation messages associated to checking the emptiness
64+
* for each corresponding field.
65+
*
66+
* @var array
67+
*/
68+
protected $_allowEmptyMessages= [];
69+
70+
5471
/**
5572
* Returns an array of fields that have failed validation. On the current model. This method will
5673
* actually run validation rules over data, not just return the messages.
@@ -62,11 +79,17 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
6279
*/
6380
public function errors(array $data, $newRecord = true) {
6481
$errors = [];
82+
$requiredMessage = __d('cake', 'This field is required');
83+
$emptyMessage = __d('cake', 'This field cannot be left empty');
84+
6585
foreach ($this->_fields as $name => $field) {
6686
$keyPresent = array_key_exists($name, $data);
6787

6888
if (!$keyPresent && !$this->_checkPresence($field, $newRecord)) {
69-
$errors[$name][] = __d('cake', 'This field is required');
89+
$message = isset($this->_presenceMessages[$name])
90+
? __d($this->_validationDomain, $this->_presenceMessages[$name])
91+
: $requiredMessage;
92+
$errors[$name][] = $message;
7093
continue;
7194
}
7295

@@ -78,7 +101,10 @@ public function errors(array $data, $newRecord = true) {
78101
$isEmpty = $this->_fieldIsEmpty($data[$name]);
79102

80103
if (!$canBeEmpty && $isEmpty) {
81-
$errors[$name][] = __d('cake', 'This field cannot be left empty');
104+
$message = isset($this->_allowEmptyMessages[$name])
105+
? __d($this->_validationDomain, $this->_allowEmptyMessages[$name])
106+
: $emptyMessage;
107+
$errors[$name][] = $message;
82108
continue;
83109
}
84110

@@ -293,10 +319,15 @@ public function remove($field, $rule = null) {
293319
*
294320
* @param string $field the name of the field
295321
* @param boolean|string $mode Valid values are true, false, 'create', 'update'
322+
* @param string $message The validation message to show when if the field presence
323+
* is required.
296324
* @return Validator this instance
297325
*/
298-
public function validatePresence($field, $mode = true) {
326+
public function validatePresence($field, $mode = true, $message = null) {
299327
$this->field($field)->isPresenceRequired($mode);
328+
if ($message) {
329+
$this->_presenceMessages[$field] = $message;
330+
}
300331
return $this;
301332
}
302333

@@ -306,10 +337,15 @@ public function validatePresence($field, $mode = true) {
306337
*
307338
* @param string $field the name of the field
308339
* @param boolean|string $mode Valid values are true, false, 'create', 'update'
340+
* @param string $message The validation message to show when if the field is not
341+
* allowed to be empty.
309342
* @return Validator this instance
310343
*/
311-
public function allowEmpty($field, $mode = true) {
344+
public function allowEmpty($field, $mode = true, $message = null) {
312345
$this->field($field)->isEmptyAllowed($mode);
346+
if ($message) {
347+
$this->_allowEmptyMessages[$field] = $message;
348+
}
313349
return $this;
314350
}
315351

tests/TestCase/Validation/ValidatorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@ public function testErrorsWithPresenceRequired() {
154154
$this->assertEmpty($validator->errors(['foo' => 'bar']));
155155
}
156156

157+
/**
158+
* Tests custom error messages generated when a field presence is required
159+
*
160+
* @return void
161+
*/
162+
public function testCustomErrorsWithPresenceRequired() {
163+
$validator = new Validator;
164+
$validator->validatePresence('title', true, 'Custom message');
165+
$errors = $validator->errors(['foo' => 'something']);
166+
$expected = ['title' => ['Custom message']];
167+
$this->assertEquals($expected, $errors);
168+
}
169+
157170
/**
158171
* Tests the allowEmpty method
159172
*
@@ -228,6 +241,19 @@ public function testErrorsWithEmptyNotAllowed() {
228241
$this->assertEmpty($errors);
229242
}
230243

244+
/**
245+
* Tests custom error mesages generated when a field is not allowed to be empty
246+
*
247+
* @return void
248+
*/
249+
public function testCustomErrorsWithEmptyNotAllowed() {
250+
$validator = new Validator;
251+
$validator->allowEmpty('title', false, 'Custom message');
252+
$errors = $validator->errors(['title' => '']);
253+
$expected = ['title' => ['Custom message']];
254+
$this->assertEquals($expected, $errors);
255+
}
256+
231257
/**
232258
* Tests errors generated when a field is allowed to be empty
233259
*

0 commit comments

Comments
 (0)