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) {
36 var tests = $e.data('validate').split(/ +/);
38 for (var test_idx in tests) {
39 var test = tests[test_idx];
43 if (ns.checks[test]) {
44 if (!ns.checks[test]($e))
47 var error = "kivi.validate_form: unknown test '" + test + "' for element ID '" + $e.prop('id') + "'";
58 required: function($e) {
59 if ($e.val() === '') {
60 ns.annotate($e, kivi.t8("This field must not be empty."));
67 number: function($e) {
68 var number_string = $e.val();
70 var parsed_number = kivi.parse_amount(number_string);
72 if (parsed_number === null) {
77 if (parsed_number === undefined) {
78 ns.annotate($e, kivi.t8('Wrong number format (#1)', [ kivi.myconfig.numberformat ]));
82 var formatted_number = kivi.format_amount(parsed_number);
83 if (formatted_number != number_string)
84 $e.val(formatted_number);
90 var date_string = $e.val();
92 var parsed_date = kivi.parse_date(date_string);
94 if (parsed_date === null) {
99 if (parsed_date === undefined) {
100 ns.annotate($e, kivi.t8('Wrong date format (#1)', [ kivi.myconfig.dateformat ]));
104 var formatted_date = kivi.format_date(parsed_date);
105 if (formatted_date != date_string)
106 $e.val(formatted_date);
112 var time_string = $e.val();
114 var parsed_time = kivi.parse_time(time_string);
115 if (parsed_time === null) {
120 if (parsed_time === undefined) {
121 ns.annotate($e, kivi.t8('Wrong time format (#1)', [ kivi.myconfig.timeformat ]));
125 var formatted_time = kivi.format_time(parsed_time);
126 if (formatted_time != time_string)
127 $e.val(formatted_time);
134 ns.annotate = function($e, error) {
136 $e.addClass('kivi-validator-invalid');
137 if ($e.hasClass('tooltipstered'))
138 $e.tooltipster('destroy');
140 if ($e.data('title'))
141 error = $e.data('title') + ': ' + error;
145 theme: 'tooltipster-light',
147 $e.tooltipster('show');
149 $e.removeClass('kivi-validator-invalid');
150 if ($e.hasClass('tooltipstered'))
151 $e.tooltipster('destroy');
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) });
161 ns.init = ns.reinit_widgets;