@@ -51,6 +51,23 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
51
51
*/
52
52
protected $ _validationDomain = 'default ' ;
53
53
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
+
54
71
/**
55
72
* Returns an array of fields that have failed validation. On the current model. This method will
56
73
* actually run validation rules over data, not just return the messages.
@@ -62,11 +79,17 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
62
79
*/
63
80
public function errors (array $ data , $ newRecord = true ) {
64
81
$ errors = [];
82
+ $ requiredMessage = __d ('cake ' , 'This field is required ' );
83
+ $ emptyMessage = __d ('cake ' , 'This field cannot be left empty ' );
84
+
65
85
foreach ($ this ->_fields as $ name => $ field ) {
66
86
$ keyPresent = array_key_exists ($ name , $ data );
67
87
68
88
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 ;
70
93
continue ;
71
94
}
72
95
@@ -78,7 +101,10 @@ public function errors(array $data, $newRecord = true) {
78
101
$ isEmpty = $ this ->_fieldIsEmpty ($ data [$ name ]);
79
102
80
103
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 ;
82
108
continue ;
83
109
}
84
110
@@ -293,10 +319,15 @@ public function remove($field, $rule = null) {
293
319
*
294
320
* @param string $field the name of the field
295
321
* @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.
296
324
* @return Validator this instance
297
325
*/
298
- public function validatePresence ($ field , $ mode = true ) {
326
+ public function validatePresence ($ field , $ mode = true , $ message = null ) {
299
327
$ this ->field ($ field )->isPresenceRequired ($ mode );
328
+ if ($ message ) {
329
+ $ this ->_presenceMessages [$ field ] = $ message ;
330
+ }
300
331
return $ this ;
301
332
}
302
333
@@ -306,10 +337,15 @@ public function validatePresence($field, $mode = true) {
306
337
*
307
338
* @param string $field the name of the field
308
339
* @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.
309
342
* @return Validator this instance
310
343
*/
311
- public function allowEmpty ($ field , $ mode = true ) {
344
+ public function allowEmpty ($ field , $ mode = true , $ message = null ) {
312
345
$ this ->field ($ field )->isEmptyAllowed ($ mode );
346
+ if ($ message ) {
347
+ $ this ->_allowEmptyMessages [$ field ] = $ message ;
348
+ }
313
349
return $ this ;
314
350
}
315
351
0 commit comments