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" must be set to a human-readable name of the field
9 // that can be shown as part of an error message.
11 // Supported validation tests are:
12 // - "required": the field must be set (its .val() must not be empty)
14 // The validation will abort and return "false" as soon as
15 // validation routine fails.
17 // The function returns "true" if all validations succeed for all
19 ns.validate_all = function(selector) {
20 selector = selector || '#form';
21 var to_check = $(selector + ' [data-validate]').toArray();
23 for (var to_check_idx in to_check)
24 if (!ns.validate($(to_check[to_check_idx]))) {
25 $(to_check[to_check_idx]).focus();
32 ns.validate = function($e) {
33 var tests = $e.data('validate').split(/ +/);
35 for (var test_idx in tests) {
36 var test = tests[test_idx];
40 if (ns.checks[test]) {
41 if (!ns.checks[test]($e))
44 var error = "kivi.validate_form: unknown test '" + test + "' for element ID '" + $e.prop('id') + "'";
55 required: function($e) {
56 if ($e.val() === '') {
57 ns.annotate($e, kivi.t8("This field must not be empty."));
64 number: function($e) {
65 var number_string = $e.val();
67 var parsed_number = kivi.parse_amount(number_string);
69 if (parsed_number === null) {
74 if (parsed_number === undefined) {
75 ns.annotate($e, kivi.t8('Wrong number format (#1)', [ kivi.myconfig.numberformat ]));
79 var formatted_number = kivi.format_amount(parsed_number);
80 if (formatted_number != number_string)
81 $e.val(formatted_number);
87 var date_string = $e.val();
89 var parsed_date = kivi.parse_date(date_string);
91 if (parsed_date === null) {
96 if (parsed_date === undefined) {
97 ns.annotate($e, kivi.t8('Wrong date format (#1)', [ kivi.myconfig.dateformat ]));
101 var formatted_date = kivi.format_date(parsed_date);
102 if (formatted_date != date_string)
103 $e.val(formatted_date);
109 var time_string = $e.val();
111 var parsed_time = kivi.parse_time(time_string);
112 if (parsed_time === null) {
117 if (parsed_time === undefined) {
118 ns.annotate($e, kivi.t8('Wrong time format (#1)', [ kivi.myconfig.timeformat ]));
122 var formatted_time = kivi.format_time(parsed_time);
123 if (formatted_time != time_string)
124 $e.val(formatted_time);
131 ns.annotate = function($e, error) {
133 $e.addClass('kivi-validator-invalid');
134 if ($e.hasClass('tooltipstered'))
135 $e.tooltipster('destroy');
139 theme: 'tooltipster-light',
141 $e.tooltipster('show');
143 $e.removeClass('kivi-validator-invalid');
144 if ($e.hasClass('tooltipstered'))
145 $e.tooltipster('destroy');
149 ns.reinit_widgets = function() {
150 kivi.run_once_for('[data-validate]', 'data-validate', function(elt) {
151 $(elt).change(function(event){ ns.validate($(elt), event) });
155 ns.init = ns.reinit_widgets;