WebUI: Add positive number validator

Add new validator which inherits from integer validator
and checks whether the integer is positive.

https://pagure.io/freeipa/issue/6980

Reviewed-By: Felipe Volpone <felipevolpone@gmail.com>
This commit is contained in:
Pavel Vomacka 2017-07-11 10:46:36 +02:00 committed by Martin Babinsky
parent 7f10a5145c
commit 3cac851498
2 changed files with 44 additions and 0 deletions

View File

@ -1049,9 +1049,51 @@ field.validator = IPA.validator = function(spec) {
return that.true_result();
};
that.integer_validate = that.validate;
return that;
};
/**
* Javascript positive integer validator
*
* It allows to insert only positive integer.
*
* @class
* @alternateClassName IPA.positive_integer_validator
* @extends IPA.validator
*/
field.positive_integer_validator = IPA.positive_integer_validator = function(spec) {
var that = IPA.integer_validator(spec);
/**
* @inheritDoc
*/
that.validate = function(value) {
var integer_check = that.integer_validate(value);
if (!integer_check.valid) {
return integer_check;
}
var num = parseInt(value);
if (num <= 0) {
return that.false_result(
text.get('@i18n:widget.validation.positive_number'));
}
return that.true_result();
};
return that;
};
/**
* Metadata validator
*
@ -1871,6 +1913,7 @@ field.register = function() {
v.register('unsupported', field.unsupported_validator);
v.register('same_password', field.same_password_validator);
v.register('integer', field.integer_validator);
v.register('positive_integer', field.positive_integer_validator);
l.register('adapter', field.Adapter);
l.register('object_adapter', field.ObjectAdapter);

View File

@ -982,6 +982,7 @@ class i18n_messages(Command):
"min_value": _("Minimum value is ${value}"),
"net_address": _("Not a valid network address (examples: 2001:db8::/64, 192.0.2.0/24)"),
"parse": _("Parse error"),
"positive_number": _("Must be a positive number"),
"port": _("'${port}' is not a valid port"),
"required": _("Required field"),
"unsupported": _("Unsupported value"),