Entitlement quantity validation.

The widget base class has been modified to validate integer value
if the type is specified in the metadata. This is used to validate
entitlement quantity.
This commit is contained in:
Endi S. Dewata 2011-04-26 16:21:25 -05:00 committed by Endi Sukma Dewata
parent 000ba0531e
commit f256b8857f
7 changed files with 101 additions and 38 deletions

View File

@ -74,11 +74,19 @@ IPA.dialog = function(spec) {
that.fields_by_name[field.name] = field;
};
that.field = function(field){
that.field = function(field) {
that.add_field(field);
return that;
};
that.is_valid = function() {
for (var i=0; i<that.fields.length; i++) {
var field = that.fields[i];
if (!field.valid) return false;
}
return true;
};
that.text = function(name){
that.field(IPA.text_widget({
name: name,

View File

@ -69,7 +69,7 @@ IPA.entity_factories.dnszone = function() {
};
IPA.force_dnszone_add_checkbox_widget = function (spec){
var param_info = IPA.get_method_param('dnszone_add', 'force');
var param_info = IPA.get_method_option('dnszone_add', 'force');
spec.name = 'force';
spec.label = param_info.label;
spec.tooltip = param_info.doc;

View File

@ -86,7 +86,7 @@ IPA.entity_factories.entitle = function() {
},
{
name: 'password',
label: IPA.get_method_param('entitle_register', 'password').label,
label: IPA.get_method_option('entitle_register', 'password').label,
type: 'password',
undo: false
}
@ -114,7 +114,8 @@ IPA.entity_factories.entitle = function() {
{
name: 'quantity',
label: 'Quantity',
undo: false
undo: false,
metadata: IPA.get_method_arg('entitle_consume', 'quantity')
}
]
}).
@ -261,7 +262,6 @@ IPA.entitle.search_facet = function(spec) {
that.setup = function(container) {
that.search_facet_setup(container);
var buttons = that.entity_header.buttons;
@ -408,6 +408,7 @@ IPA.entitle.certificate_column = function(spec) {
var that = IPA.column(spec);
that.setup = function(container, record) {
container.empty();
var certificate = record[that.name];
@ -527,6 +528,11 @@ IPA.entitle.consume_dialog = function(spec) {
var that = IPA.dialog(spec);
that.add_button('Consume', function() {
if (!that.is_valid()) {
return;
}
var record = {};
that.save(record);

View File

@ -122,7 +122,7 @@ IPA.utc_date_column_format = function(value){
IPA.force_host_add_checkbox_widget = function (spec){
var param_info = IPA.get_method_param('host_add', 'force');
var param_info = IPA.get_method_option('host_add', 'force');
spec.name = 'force';
spec.label = param_info.label;
spec.tooltip = param_info.doc;

View File

@ -539,7 +539,28 @@ IPA.get_entity_param = function(entity_name, name) {
return null;
};
IPA.get_method_param = function(method_name, name) {
IPA.get_method_arg = function(method_name, name) {
var metadata = IPA.metadata.methods[method_name];
if (!metadata) {
return null;
}
var args = metadata.takes_args;
if (!args) {
return null;
}
for (var i=0; i<args.length; i++) {
if (args[i].name === name) {
return args[i];
}
}
return null;
};
IPA.get_method_option = function(method_name, name) {
var metadata = IPA.metadata.methods[method_name];
if (!metadata) {

View File

@ -127,8 +127,8 @@ IPA.service_add_dialog = function(spec) {
field(
IPA.checkbox_widget({
name: 'force',
label: IPA.get_method_param('service_add', 'force').label,
tooltip: IPA.get_method_param('service_add', 'force').doc,
label: IPA.get_method_option('service_add', 'force').label,
tooltip: IPA.get_method_option('service_add', 'force').doc,
undo: false
}));

View File

@ -57,10 +57,12 @@ IPA.widget = function(spec) {
that.load = spec.load || load;
that.save = spec.save || save;
that.update = spec.update || update;
that.validate_input = spec.validate_input || validate_input;
that.valid = true;
that.param_info = spec.param_info;
that.metadata = spec.metadata;
that.values = [];
that.valid = true;
that.__defineGetter__("entity_name", function(){
return that._entity_name;
@ -73,29 +75,58 @@ IPA.widget = function(spec) {
/*returns true and clears the error message if the field value passes
the validation pattern. If the field value does not pass validation,
displays the error message and returns false. */
function validate_input(text) {
if (!(that.param_info && that.param_info.pattern)) {
that.valid = true;
that.validate = function() {
that.hide_error();
that.valid = true;
var values = that.save();
if (!values || !values.length) {
return;
}
var error_link = that.get_error_link();
if (!error_link) {
that.valid = true;
var value = values[0];
if (!value) {
return;
}
var regex = new RegExp( that.param_info.pattern );
//If the field is empty, don't validate
if ( !text || text.match(regex) ) {
error_link.css('display', 'none');
that.valid = true;
} else {
error_link.css('display', 'block');
if (that.param_info.pattern_errmsg) {
error_link.html(that.param_info.pattern_errmsg);
if (that.metadata) {
if (that.metadata.type == 'int') {
if (!value.match(/^-?\d+$/)) {
that.valid = false;
// TODO: I18n
that.show_error('must be an integer');
return;
}
if (that.metadata.minvalue && value < that.metadata.minvalue) {
that.valid = false;
// TODO: I18n
that.show_error('minimum value is '+that.metadata.minvalue);
return;
}
if (that.metadata.maxvalue && value > that.metadata.maxvalue) {
that.valid = false;
// TODO: I18n
that.show_error('maximum value is '+that.metadata.maxvalue);
return;
}
}
that.valid = false;
}
}
if (that.param_info) {
if (that.param_info.pattern) {
var regex = new RegExp(that.param_info.pattern);
if (!value.match(regex)) {
that.valid = false;
that.show_error(that.param_info.pattern_errmsg);
return;
}
}
}
};
function init() {
if (that.entity_name) {
@ -247,12 +278,13 @@ IPA.widget = function(spec) {
return $('span[name="error_link"]', that.container);
};
that.show_error_link = function() {
that.show_error = function(message) {
var error_link = that.get_error_link();
error_link.css('display', 'inline');
error_link.html(message);
error_link.css('display', 'block');
};
that.hide_error_link = function() {
that.hide_error = function() {
var error_link = that.get_error_link();
error_link.css('display', 'none');
};
@ -323,8 +355,7 @@ IPA.text_widget = function(spec) {
if (that.undo) {
that.show_undo();
}
var value = $(this).val();
that.validate_input(value);
that.validate();
});
var undo = that.get_undo();
@ -539,8 +570,7 @@ IPA.multivalued_text_widget = function(spec) {
remove_link.css('display', 'inline');
}
}
var value = $(this).val();
that.validate_input(value);
that.validate();
});
remove_link.click(function() {
@ -965,9 +995,7 @@ IPA.textarea_widget = function (spec) {
var input = $('textarea[name="'+that.name+'"]', that.container);
input.keyup(function() {
that.show_undo();
var value = $(this).val();
that.validate_input(value);
that.validate();
});