js-Validator: Kommentar-Update
[kivitendo-erp.git] / js / kivi.Validator.js
1 namespace("kivi.Validator", function(ns) {
2   "use strict";
3
4   // Performs various validation steps on the descendants of
5   // 'selector'. Elements that should be validated must have an
6   // attribute named "data-validate" which is set to a space-separated
7   // list of tests to perform. Additionally, the attribute
8   // "data-title" can be set to a human-readable name of the field
9   // that can be shown in front of an error message.
10   //
11   // Supported validation tests are:
12   // - "required": the field must be set (its .val() must not be empty)
13   // - "number": the field must be in number format (its .val() must in the right format)
14   // - "date": the field must be in date format (its .val() must in the right format)
15   // - "time": the field must be in time format (its .val() must in the right format)
16   //
17   // The validation will abort and return "false" as soon as
18   // validation routine fails.
19   //
20   // The function returns "true" if all validations succeed for all
21   // elements.
22   ns.validate_all = function(selector) {
23     selector = selector || '#form';
24     var to_check = $(selector + ' [data-validate]').toArray();
25
26     for (var to_check_idx in to_check)
27       if (!ns.validate($(to_check[to_check_idx]))) {
28         $(to_check[to_check_idx]).focus();
29         return false;
30       }
31
32     return true;
33   };
34
35   ns.validate = function($e) {
36     var tests = $e.data('validate').split(/ +/);
37
38     for (var test_idx in tests) {
39       var test = tests[test_idx];
40       if (!ns.checks[test])
41         continue;
42
43       if (ns.checks[test]) {
44         if (!ns.checks[test]($e))
45           return false;
46       } else {
47         var error = "kivi.validate_form: unknown test '" + test + "' for element ID '" + $e.prop('id') + "'";
48         console.error(error);
49         alert(error);
50         return false;
51       }
52     }
53
54     return true;
55   }
56
57   ns.checks = {
58     required: function($e) {
59       if ($e.val() === '') {
60         ns.annotate($e, kivi.t8("This field must not be empty."));
61         return false;
62       } else {
63         ns.annotate($e);
64         return true;
65       }
66     },
67     number: function($e) {
68       var number_string = $e.val();
69
70       var parsed_number = kivi.parse_amount(number_string);
71
72       if (parsed_number === null) {
73         $e.val('');
74         ns.annotate($e);
75         return true;
76       } else
77       if (parsed_number === undefined) {
78         ns.annotate($e, kivi.t8('Wrong number format (#1)', [ kivi.myconfig.numberformat ]));
79         return false;
80       } else
81       {
82         var formatted_number = kivi.format_amount(parsed_number);
83         if (formatted_number != number_string)
84           $e.val(formatted_number);
85         ns.annotate($e);
86         return true;
87       }
88     },
89     date: function($e) {
90       var date_string = $e.val();
91
92       var parsed_date = kivi.parse_date(date_string);
93
94       if (parsed_date === null) {
95         $e.val('');
96         ns.annotate($e);
97         return true;
98       } else
99       if (parsed_date === undefined) {
100         ns.annotate($e, kivi.t8('Wrong date format (#1)', [ kivi.myconfig.dateformat ]));
101         return false;
102       } else
103       {
104         var formatted_date = kivi.format_date(parsed_date);
105         if (formatted_date != date_string)
106           $e.val(formatted_date);
107         ns.annotate($e);
108         return true;
109       }
110     },
111     time: function($e) {
112       var time_string = $e.val();
113
114       var parsed_time = kivi.parse_time(time_string);
115       if (parsed_time === null) {
116         $e.val('');
117         ns.annotate($e);
118         return true;
119       } else
120       if (parsed_time === undefined) {
121         ns.annotate($e, kivi.t8('Wrong time format (#1)', [ kivi.myconfig.timeformat ]));
122         return false;
123       } else
124       {
125         var formatted_time = kivi.format_time(parsed_time);
126         if (formatted_time != time_string)
127           $e.val(formatted_time);
128         ns.annotate($e);
129         return true;
130       }
131     }
132   };
133
134   ns.annotate = function($e, error) {
135     if (error) {
136       $e.addClass('kivi-validator-invalid');
137       if ($e.hasClass('tooltipstered'))
138         $e.tooltipster('destroy');
139
140       if ($e.data('title'))
141         error = $e.data('title') + ': ' + error;
142
143       $e.tooltipster({
144         content: error,
145         theme: 'tooltipster-light',
146       });
147       $e.tooltipster('show');
148     } else {
149       $e.removeClass('kivi-validator-invalid');
150       if ($e.hasClass('tooltipstered'))
151         $e.tooltipster('destroy');
152     }
153   };
154
155   ns.reinit_widgets = function() {
156     kivi.run_once_for('[data-validate]', 'data-validate', function(elt) {
157       $(elt).change(function(event){ ns.validate($(elt), event) });
158     });
159   }
160
161   ns.init = ns.reinit_widgets;
162
163   $(ns.init);
164 });