Skip to content
This repository
Browse code

add widget.formatValue and implement custom formatValue for date fields

this puts the corrent value in the value attribute for date fields
  • Loading branch information...
commit 74abee99596056324b27d673135f7303c6e3634b 1 parent aaeb266
Christian Tellnes authored March 17, 2013
25  lib/widgets.js
@@ -64,6 +64,9 @@ var input = function (type) {
64 64
             }
65 65
             return attrs;
66 66
         }, {});
  67
+        w.formatValue = function (value) {
  68
+            return value || null;
  69
+        };
67 70
         w.toHTML = function (name, f) {
68 71
             if (!f) { f = {}; }
69 72
             return singleTag('input', [{
@@ -71,7 +74,7 @@ var input = function (type) {
71 74
                 name: name,
72 75
                 id: f.id || true,
73 76
                 classes: w.classes,
74  
-                value: f.value || null
  77
+                value: w.formatValue(f.value)
75 78
             }, userAttrs]);
76 79
         };
77 80
         w.getDataRegExp = function () {
@@ -88,7 +91,25 @@ exports.text = input('text');
88 91
 exports.password = input('password');
89 92
 exports.hidden = input('hidden');
90 93
 exports.color = input('color');
91  
-exports.date = input('date');
  94
+
  95
+var dateWidget = input('date');
  96
+exports.date = function (opt) {
  97
+    var w = dateWidget(opt);
  98
+    w.formatValue = function (value) {
  99
+        if (!value) {
  100
+            return null;
  101
+        }
  102
+
  103
+        var date = value instanceof Date ? value : new Date(value);
  104
+
  105
+        if (isNaN(date.getTime())) {
  106
+            return null;
  107
+        }
  108
+
  109
+        return date.toISOString().slice(0, 10);
  110
+    };
  111
+    return w;
  112
+};
92 113
 
93 114
 exports.checkbox = function (opt) {
94 115
     if (!opt) { opt = {}; }
25  test/test-widgets.js
@@ -18,6 +18,8 @@ var test_input = function (type) {
18 18
             '<input type="' + type + '" name="field1" id="id_field1" value="some value" />'
19 19
         );
20 20
         test.equals(forms.widgets[type]().type, type);
  21
+        test.equals(forms.widgets[type]().formatValue('hello'), 'hello');
  22
+        test.strictEqual(forms.widgets[type]().formatValue(false), null);
21 23
         test.done();
22 24
     };
23 25
 };
@@ -26,7 +28,28 @@ exports.text = test_input('text');
26 28
 exports.password = test_input('password');
27 29
 exports.hidden = test_input('hidden');
28 30
 exports.color = test_input('color');
29  
-exports.date = test_input('date');
  31
+
  32
+exports.date = function (test) {
  33
+    var w = forms.widgets.date();
  34
+    test.equals(w.formatValue(new Date(Date.UTC(2013, 2, 1))), '2013-03-01');
  35
+    test.equals(w.formatValue('2013-03-02'), '2013-03-02');
  36
+    test.strictEqual(w.formatValue('invalid'), null);
  37
+
  38
+    test.equals(w.type, 'date');
  39
+
  40
+    test.equals(
  41
+        w.toHTML('field1'),
  42
+        '<input type="date" name="field1" id="id_field1" />'
  43
+    );
  44
+
  45
+    test.equals(
  46
+        w.toHTML('field1', {value: '2013-03-03'}),
  47
+        '<input type="date" name="field1" id="id_field1" value="2013-03-03" />'
  48
+    );
  49
+
  50
+
  51
+    test.done();
  52
+};
30 53
 
31 54
 exports.checkbox = function (test) {
32 55
     test.equals(

0 notes on commit 74abee9

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