Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enhancing validation messages to have placeholders. Closes #1855

  • Loading branch information...
commit b15c77ecc3bc5c6183518a1c7b24b9f9a2aff810 1 parent f9373ff
@ADmad ADmad authored
View
9 lib/Cake/Console/Command/Task/ExtractTask.php
@@ -390,7 +390,12 @@ protected function _processValidationRules($field, $rules, $file, $domain) {
foreach ($rules as $rule => $validateProp) {
if (isset($validateProp['message'])) {
- $this->_strings[$domain][$validateProp['message']][$file][] = 'validation for field ' . $field;
+ if (is_array($validateProp['message'])) {
+ $message = $validateProp['message'][0];
+ } else {
+ $message = $validateProp['message'];
+ }
+ $this->_strings[$domain][$message][$file][] = 'validation for field ' . $field;
}
}
}
@@ -504,7 +509,7 @@ protected function _writeHeader() {
$output .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n";
return $output;
}
-
+
/**
* Get the strings from the position forward
*
View
9 lib/Cake/Model/Model.php
@@ -2271,7 +2271,7 @@ public function buildQuery($type = 'first', $query = array()) {
return null;
}
}
-
+
return $query;
}
@@ -2754,6 +2754,13 @@ public function invalidFields($options = array()) {
} else {
$validator['message'] = __d($validationDomain, $message);
}
+ } elseif (is_array($validator['message'])) {
+ if (count($validator['message']) > 1) {
+ $args = array_slice($validator['message'], 1);
+ } else {
+ $args = $validator['rule'];
+ }
+ $validator['message'] = __d($validationDomain, $validator['message'][0], $args);
}
$this->invalidate($fieldName, $validator['message']);
View
3  lib/Cake/Test/Case/Console/Command/Task/ExtractTaskTest.php
@@ -293,6 +293,9 @@ public function testExtractModelValidation() {
$pattern = '#msgid "Post title is required"#';
$this->assertPattern($pattern, $result);
+ $pattern = '#msgid "You may enter up to %s chars \(minimum is %s chars\)"#';
+ $this->assertPattern($pattern, $result);
+
$pattern = '#msgid "Post body is required"#';
$this->assertPattern($pattern, $result);
View
31 lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -682,4 +682,35 @@ public function testMissingValidationErrorNoTriggering() {
$this->assertEquals($TestModel->validationErrors, array());
}
+/**
+ * Test placeholder replacement when validation message is an array
+ *
+ * @return void
+ */
+ public function testValidationMessageAsArray() {
+ $TestModel = new ValidationTest1();
+ $TestModel->create(array('title' => 'foo'));
+ $TestModel->validate = array(
+ 'title' => array(
+ 'minLength' => array(
+ 'rule' => array('minLength', 6),
+ 'message' => array('Minimum length allowed is %d chars'),
+ 'last' => false
+ ),
+ 'between' => array(
+ 'rule' => array('between', 5, 15),
+ 'message' => array('You may enter up to %s chars (minimum is %s chars)', 14, 6)
+ )
+ )
+ );
+ $TestModel->invalidFields();
+ $expected = array(
+ 'title' => array(
+ 'Minimum length allowed is 6 chars',
+ 'You may enter up to 14 chars (minimum is 6 chars)'
+ )
+ );
+ $this->assertEquals($TestModel->validationErrors, $expected);
+ }
+
}
View
14 lib/Cake/Test/test_app/Model/PersisterOne.php
@@ -27,10 +27,16 @@ class PersisterOne extends AppModel {
public $hasMany = array('Comment', 'TestPlugin.TestPluginComment');
public $validate = array(
'title' => array(
- 'rule' => array('custom', '.*'),
- 'allowEmpty' => true,
- 'required' => false,
- 'message' => 'Post title is required'
+ 'custom' => array(
+ 'rule' => array('custom', '.*'),
+ 'allowEmpty' => true,
+ 'required' => false,
+ 'message' => 'Post title is required'
+ ),
+ 'between' => array(
+ 'rule' => array('between', 5, 15),
+ 'message' => array('You may enter up to %s chars (minimum is %s chars)', 14, 6)
+ )
),
'body' => array(
'first_rule' => array(
Please sign in to comment.
Something went wrong with that request. Please try again.