1 namespace("kivi.Validator", function(ns) {
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.
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)
17 // The validation will abort and return "false" as soon as
18 // validation routine fails.
20 // The function returns "true" if all validations succeed for all
22 ns.validate_all = function(selector) {
23 selector = selector || '#form';
24 var to_check = $(selector + ' [data-validate]').toArray();
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();
35 ns.validate = function($e) {
37 if ($e.data('ckeditorInstance')) {
38 $e_annotate = $($e.data('ckeditorInstance').editable().$);
40 $e_annotate.data('title', $e.data('title'));
42 var tests = $e.data('validate').split(/ +/);
44 for (var test_idx in tests) {
45 var test = tests[test_idx];
49 if (ns.checks[test]) {
50 if (!ns.checks[test]($e, $e_annotate))
53 var error = "kivi.validate_form: unknown test '" + test + "' for element ID '" + $e.prop('id') + "'";
64 required: function($e, $e_annotate) {
65 $e_annotate = $e_annotate || $e;
67 if ($e.val() === '') {
68 ns.annotate($e_annotate, kivi.t8("This field must not be empty."));
71 ns.annotate($e_annotate);
75 number: function($e, $e_annotate) {
76 $e_annotate = $e_annotate || $e;
78 var number_string = $e.val();
80 var parsed_number = kivi.parse_amount(number_string);
82 if (parsed_number === null) {
84 ns.annotate($e_annotate);
87 if (parsed_number === undefined) {
88 ns.annotate($e_annotate, kivi.t8('Wrong number format (#1)', [ kivi.myconfig.numberformat ]));
92 var formatted_number = kivi.format_amount(parsed_number);
93 if (formatted_number != number_string)
94 $e.val(formatted_number);
95 ns.annotate($e_annotate);
99 date: function($e, $e_annotate) {
100 $e_annotate = $e_annotate || $e;
102 var date_string = $e.val();
104 var parsed_date = kivi.parse_date(date_string);
106 if (parsed_date === null) {
108 ns.annotate($e_annotate);
111 if (parsed_date === undefined) {
112 ns.annotate($e_annotate, kivi.t8('Wrong date format (#1)', [ kivi.myconfig.dateformat ]));
116 var formatted_date = kivi.format_date(parsed_date);
117 if (formatted_date != date_string)
118 $e.val(formatted_date);
119 ns.annotate($e_annotate);
123 time: function($e, $e_annotate) {
124 $e_annotate = $e_annotate || $e;
126 var time_string = $e.val();
128 var parsed_time = kivi.parse_time(time_string);
129 if (parsed_time === null) {
131 ns.annotate($e_annotate);
134 if (parsed_time === undefined) {
135 ns.annotate($e_annotate, kivi.t8('Wrong time format (#1)', [ kivi.myconfig.timeformat ]));
139 var formatted_time = kivi.format_time(parsed_time);
140 if (formatted_time != time_string)
141 $e.val(formatted_time);
142 ns.annotate($e_annotate);
148 ns.annotate = function($e, error) {
150 $e.addClass('kivi-validator-invalid');
151 if ($e.hasClass('tooltipstered'))
152 $e.tooltipster('destroy');
154 if ($e.data('title'))
155 error = $e.data('title') + ': ' + error;
159 theme: 'tooltipster-light',
161 $e.tooltipster('show');
163 $e.removeClass('kivi-validator-invalid');
164 if ($e.hasClass('tooltipstered'))
165 $e.tooltipster('destroy');
169 ns.reinit_widgets = function() {
170 kivi.run_once_for('[data-validate]', 'data-validate', function(elt) {
171 $(elt).change(function(event){ ns.validate($(elt), event) });
175 ns.init = ns.reinit_widgets;