Skip to content
This repository
Browse code

Move the ‘Validation.prototype.renderErrorMessage’ method into a sepa…

…rate file
  • Loading branch information...
commit c790fd5c105b1518f992463c6def26ba5dee8842 1 parent a87600c
František Hába authored

Showing 1 changed file with 31 additions and 0 deletions. Show diff stats Hide diff stats

  1. 31  src/engines/json/renderErrorMessage.js
31  src/engines/json/renderErrorMessage.js
... ...
@@ -0,0 +1,31 @@
  1
+/**
  2
+ * RenderErrorMessage
  3
+ *
  4
+ * @param {string} attributeName
  5
+ * @param {object} templateData
  6
+ */
  7
+Validation.prototype.renderErrorMessage = function(attributeName, templateData) {
  8
+  
  9
+  // Gets an error message
  10
+  var errorMessage = this.messages[attributeName];
  11
+  
  12
+  // If the error message is a function
  13
+  if (typeof errorMessage === 'function') {
  14
+    return errorMessage(
  15
+      templateData.property,
  16
+      templateData.propertyValue,
  17
+      templateData.attribute
  18
+    );
  19
+  }
  20
+
  21
+  // If the error message is a string
  22
+  if (typeof errorMessage === 'string') {
  23
+    each(templateData, function(key, value) {
  24
+      errorMessage = errorMessage.replace(new RegExp('{{' + key + '}}', 'g'), value);
  25
+    });
  26
+    return errorMessage.replace(/\s+/g, ' ');
  27
+  }
  28
+
  29
+  return '';
  30
+
  31
+};

0 notes on commit c790fd5

Please sign in to comment.
Something went wrong with that request. Please try again.