kivitendo/js/kivi.Validator.js @ f1fc6f27
ccfd9aac | Sven Schöling | namespace("kivi.Validator", function(ns) {
|
||
"use strict";
|
||||
// Performs various validation steps on the descendants of
|
||||
// 'selector'. Elements that should be validated must have an
|
||||
// attribute named "data-validate" which is set to a space-separated
|
||||
// list of tests to perform. Additionally, the attribute
|
||||
cda08805 | Bernd Bleßmann | // "data-title" can be set to a human-readable name of the field
|
||
// that can be shown in front of an error message.
|
||||
ccfd9aac | Sven Schöling | //
|
||
// Supported validation tests are:
|
||||
// - "required": the field must be set (its .val() must not be empty)
|
||||
cda08805 | Bernd Bleßmann | // - "number": the field must be in number format (its .val() must in the right format)
|
||
// - "date": the field must be in date format (its .val() must in the right format)
|
||||
// - "time": the field must be in time format (its .val() must in the right format)
|
||||
ccfd9aac | Sven Schöling | //
|
||
// The validation will abort and return "false" as soon as
|
||||
// validation routine fails.
|
||||
//
|
||||
// The function returns "true" if all validations succeed for all
|
||||
// elements.
|
||||
ns.validate_all = function(selector) {
|
||||
selector = selector || '#form';
|
||||
var to_check = $(selector + ' [data-validate]').toArray();
|
||||
for (var to_check_idx in to_check)
|
||||
67dcea83 | Sven Schöling | if (!ns.validate($(to_check[to_check_idx]))) {
|
||
$(to_check[to_check_idx]).focus();
|
||||
ccfd9aac | Sven Schöling | return false;
|
||
67dcea83 | Sven Schöling | }
|
||
ccfd9aac | Sven Schöling | |||
return true;
|
||||
};
|
||||
ns.validate = function($e) {
|
||||
71d45396 | Sven Schöling | /*
|
||
4cc35cdc | Bernd Bleßmann | var $e_annotate;
|
||
if ($e.data('ckeditorInstance')) {
|
||||
$e_annotate = $($e.data('ckeditorInstance').editable().$);
|
||||
if ($e.data('title'))
|
||||
$e_annotate.data('title', $e.data('title'));
|
||||
71d45396 | Sven Schöling | }*/
|
||
ccfd9aac | Sven Schöling | var tests = $e.data('validate').split(/ +/);
|
||
for (var test_idx in tests) {
|
||||
var test = tests[test_idx];
|
||||
fe249cf4 | Sven Schöling | if (!ns.checks[test])
|
||
continue;
|
||||
ccfd9aac | Sven Schöling | |||
if (ns.checks[test]) {
|
||||
71d45396 | Sven Schöling | if (!ns.checks[test]($e))
|
||
ccfd9aac | Sven Schöling | return false;
|
||
} else {
|
||||
var error = "kivi.validate_form: unknown test '" + test + "' for element ID '" + $e.prop('id') + "'";
|
||||
console.error(error);
|
||||
alert(error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
71d45396 | Sven Schöling | ns.val = function($e, data) {
|
||
if ($e.data('ckeditorInstance')) {
|
||||
if (data === undefined)
|
||||
return $e.data('ckeditorInstance').getData()
|
||||
else
|
||||
$e.data('ckeditorInstance').setData(data)
|
||||
} else {
|
||||
if (data === undefined)
|
||||
return $e.val();
|
||||
else
|
||||
$e.val(data);
|
||||
}
|
||||
}
|
||||
ccfd9aac | Sven Schöling | ns.checks = {
|
||
4cc35cdc | Bernd Bleßmann | required: function($e, $e_annotate) {
|
||
$e_annotate = $e_annotate || $e;
|
||||
71d45396 | Sven Schöling | if (ns.val($e) === '') {
|
||
4cc35cdc | Bernd Bleßmann | ns.annotate($e_annotate, kivi.t8("This field must not be empty."));
|
||
ccfd9aac | Sven Schöling | return false;
|
||
} else {
|
||||
4cc35cdc | Bernd Bleßmann | ns.annotate($e_annotate);
|
||
ccfd9aac | Sven Schöling | return true;
|
||
}
|
||||
},
|
||||
4cc35cdc | Bernd Bleßmann | number: function($e, $e_annotate) {
|
||
$e_annotate = $e_annotate || $e;
|
||||
71d45396 | Sven Schöling | var number_string = ns.val($e);
|
||
ccfd9aac | Sven Schöling | |||
var parsed_number = kivi.parse_amount(number_string);
|
||||
if (parsed_number === null) {
|
||||
71d45396 | Sven Schöling | ns.val($e, '');
|
||
4cc35cdc | Bernd Bleßmann | ns.annotate($e_annotate);
|
||
ccfd9aac | Sven Schöling | return true;
|
||
} else
|
||||
if (parsed_number === undefined) {
|
||||
4cc35cdc | Bernd Bleßmann | ns.annotate($e_annotate, kivi.t8('Wrong number format (#1)', [ kivi.myconfig.numberformat ]));
|
||
ccfd9aac | Sven Schöling | return false;
|
||
} else
|
||||
{
|
||||
f1fc6f27 | Tamino Steinert | let input_decimal_places = parsed_number.toString().split('.')[1];
|
||
let count_input_decimal_places = 0;
|
||||
if(input_decimal_places){
|
||||
count_input_decimal_places = input_decimal_places.length;
|
||||
}
|
||||
ccfd9aac | Sven Schöling | var formatted_number = kivi.format_amount(parsed_number);
|
||
f1fc6f27 | Tamino Steinert | if (formatted_number != number_string) {
|
||
71d45396 | Sven Schöling | ns.val($e, formatted_number);
|
||
f1fc6f27 | Tamino Steinert | if(count_input_decimal_places > decimal_places) {
|
||
kivi.display_flash('warning',kivi.t8('Input was rounded'));
|
||||
}
|
||||
}
|
||||
4cc35cdc | Bernd Bleßmann | ns.annotate($e_annotate);
|
||
ccfd9aac | Sven Schöling | return true;
|
||
}
|
||||
},
|
||||
4cc35cdc | Bernd Bleßmann | date: function($e, $e_annotate) {
|
||
$e_annotate = $e_annotate || $e;
|
||||
71d45396 | Sven Schöling | var date_string = ns.val($e);
|
||
ccfd9aac | Sven Schöling | |||
var parsed_date = kivi.parse_date(date_string);
|
||||
8a773988 | Sven Schöling | if (parsed_date === null) {
|
||
71d45396 | Sven Schöling | ns.val($e, '');
|
||
4cc35cdc | Bernd Bleßmann | ns.annotate($e_annotate);
|
||
ccfd9aac | Sven Schöling | return true;
|
||
} else
|
||||
if (parsed_date === undefined) {
|
||||
4cc35cdc | Bernd Bleßmann | ns.annotate($e_annotate, kivi.t8('Wrong date format (#1)', [ kivi.myconfig.dateformat ]));
|
||
ccfd9aac | Sven Schöling | return false;
|
||
} else
|
||||
{
|
||||
var formatted_date = kivi.format_date(parsed_date);
|
||||
if (formatted_date != date_string)
|
||||
71d45396 | Sven Schöling | ns.val($e, formatted_date);
|
||
4cc35cdc | Bernd Bleßmann | ns.annotate($e_annotate);
|
||
ccfd9aac | Sven Schöling | return true;
|
||
}
|
||||
eb474565 | Sven Schöling | },
|
||
4cc35cdc | Bernd Bleßmann | time: function($e, $e_annotate) {
|
||
$e_annotate = $e_annotate || $e;
|
||||
71d45396 | Sven Schöling | var time_string = ns.val($e);
|
||
eb474565 | Sven Schöling | |||
var parsed_time = kivi.parse_time(time_string);
|
||||
if (parsed_time === null) {
|
||||
71d45396 | Sven Schöling | ns.val($e, '');
|
||
4cc35cdc | Bernd Bleßmann | ns.annotate($e_annotate);
|
||
eb474565 | Sven Schöling | return true;
|
||
} else
|
||||
if (parsed_time === undefined) {
|
||||
4cc35cdc | Bernd Bleßmann | ns.annotate($e_annotate, kivi.t8('Wrong time format (#1)', [ kivi.myconfig.timeformat ]));
|
||
eb474565 | Sven Schöling | return false;
|
||
} else
|
||||
{
|
||||
var formatted_time = kivi.format_time(parsed_time);
|
||||
if (formatted_time != time_string)
|
||||
71d45396 | Sven Schöling | ns.val($e, formatted_time);
|
||
4cc35cdc | Bernd Bleßmann | ns.annotate($e_annotate);
|
||
eb474565 | Sven Schöling | return true;
|
||
}
|
||||
c1f05dac | Bernd Bleßmann | },
|
||
trimmed_whitespaces: function($e, $e_annotate) {
|
||||
$e_annotate = $e_annotate || $e;
|
||||
71d45396 | Sven Schöling | var string = ns.val($e);
|
||
c1f05dac | Bernd Bleßmann | |||
if ($e.hasClass('tooltipstered'))
|
||||
$e.tooltipster('destroy');
|
||||
if (string.match(/^\s|\s$/)) {
|
||||
71d45396 | Sven Schöling | ns.val($e, string.trim());
|
||
c1f05dac | Bernd Bleßmann | |||
$e.tooltipster({
|
||||
content: kivi.t8("Leading and trailing whitespaces have been removed."),
|
||||
contentAsHTML: true,
|
||||
theme: 'tooltipster-light',
|
||||
});
|
||||
$e.tooltipster('show');
|
||||
}
|
||||
return true;
|
||||
ccfd9aac | Sven Schöling | }
|
||
};
|
||||
ns.annotate = function($e, error) {
|
||||
71d45396 | Sven Schöling | // if element is ckeditor:
|
||
if ($e.data('ckeditorInstance')) {
|
||||
const $orig_e = $e;
|
||||
$e = $($orig_e.data('ckeditorInstance').ui.view.editable._editableElement);
|
||||
if ($orig_e.data('title'))
|
||||
$e.data('title', $orig_e.data('title'));
|
||||
}
|
||||
ccfd9aac | Sven Schöling | if (error) {
|
||
$e.addClass('kivi-validator-invalid');
|
||||
if ($e.hasClass('tooltipstered'))
|
||||
$e.tooltipster('destroy');
|
||||
7e435d9f | Bernd Bleßmann | if ($e.data('title'))
|
||
error = $e.data('title') + ': ' + error;
|
||||
ccfd9aac | Sven Schöling | $e.tooltipster({
|
||
content: error,
|
||||
theme: 'tooltipster-light',
|
||||
});
|
||||
$e.tooltipster('show');
|
||||
} else {
|
||||
$e.removeClass('kivi-validator-invalid');
|
||||
if ($e.hasClass('tooltipstered'))
|
||||
$e.tooltipster('destroy');
|
||||
}
|
||||
};
|
||||
ns.reinit_widgets = function() {
|
||||
kivi.run_once_for('[data-validate]', 'data-validate', function(elt) {
|
||||
$(elt).change(function(event){ ns.validate($(elt), event) });
|
||||
});
|
||||
}
|
||||
ns.init = ns.reinit_widgets;
|
||||
$(ns.init);
|
||||
});
|