/*jsl:import ipa.js */ /* Authors: * Endi Sukma Dewata * Adam Young * Pavel Zuna * Petr Vobornik * * Copyright (C) 2011 Red Hat * see file 'COPYING' for use and warranty information * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* REQUIRES: ipa.js, widget.js */ IPA.field = function(spec) { spec = spec || {}; var that = {}; that.entity = IPA.get_entity(spec.entity); that.container = null; that.name = spec.name; that.param = spec.param || spec.name; that.label = spec.label; that.tooltip = spec.tooltip; that.formatter = spec.formatter; that.widget = null; that.widget_name = spec.widget; // override the required flag in metadata that.required = spec.required; // read_only is set when widget is created that.read_only = spec.read_only; // writable is set during load that.writable = true; that.enabled = spec.enabled === undefined ? true : spec.enabled; that.undo = spec.undo === undefined ? true : spec.undo; that.metadata = spec.metadata; that.validators = spec.validators || []; that.priority = spec.priority; that.values = []; that.dirty = false; that.valid = true; that.dirty_changed = IPA.observer(); var init = function() { if (!that.metadata && that.entity) { that.metadata = IPA.get_entity_param(that.entity.name, that.param); } if (that.metadata) { if (that.label === undefined) { that.label = that.metadata.label; } if (that.tooltip === undefined) { that.tooltip = that.metadata.doc; } } that.validators.push(IPA.metadata_validator()); }; that.is_required = function() { if (that.read_only) return false; if (!that.writable) return false; if (that.required !== undefined) return that.required; return that.metadata && that.metadata.required; }; that.set_required = function(required) { that.required = required; that.update_required(); }; that.update_required = function() { if(that.widget && that.widget.set_required) { that.widget.set_required(that.is_required()); } }; that.validate_required = function() { var values = that.save(); if (IPA.is_empty(values) && that.is_required() && that.enabled) { that.valid = false; var message = IPA.get_message('widget.validation.required', "Required field"); that.show_error(message); return false; } return true; }; /** * 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. */ that.validate = function() { that.hide_error(); that.valid = true; if (!that.enabled) return that.valid; var values = that.save(); if (IPA.is_empty(values)) { return that.valid; } var value = values[0]; for (var i=0; i metadata.maxvalue) { message = IPA.messages.widget.validation.max_value; message = message.replace('${value}', metadata.maxvalue); return that.false_result(message); } } if (metadata.pattern) { var regex = new RegExp(metadata.pattern); if (!value.match(regex)) { return that.false_result(metadata.pattern_errmsg); } } return that.true_result(); }; return that; }; IPA.unsupported_validator = function(spec) { var that = IPA.validator(spec); that.unsupported = spec.unsupported || []; that.message = spec.message || IPA.messages.widget.validation.unsupported; that.validate = function(value, context) { if (IPA.is_empty(value)) return that.true_result(); if (that.unsupported.indexOf(value) > -1) return that.false_result(); return that.true_result(); }; return that; }; IPA.checkbox_field = function(spec) { spec = spec || {}; var that = IPA.field(spec); that.checked = spec.checked || false; that.widgets_created = function() { that.field_widgets_created(); that.widget.checked = that.checked; }; // a checkbox will always have a value, so it's never required that.is_required = function() { return false; }; that.checkbox_load = that.load; return that; }; IPA.checkboxes_field = function(spec) { spec = spec || {}; var that = IPA.field(spec); that.checkbox_load = that.load; /* // a checkbox will always have a value, so it's never required that.is_required = function() { return false; }; */ return that; }; IPA.radio_field = function(spec) { spec = spec || {}; var that = IPA.field(spec); // a radio will always have a value, so it's never required that.is_required = function() { return false; }; that.widgets_created = function() { that.field_widgets_created(); }; return that; }; IPA.multivalued_field = function(spec) { spec = spec || {}; var that = IPA.field(spec); that.load = function(record) { that.field_load(record); }; that.test_dirty = function() { var dirty = that.field_test_dirty(); dirty = dirty || that.widget.test_dirty(); //also checks order return dirty; }; that.validate = function() { var values = that.save(); return that.validate_core(values); }; that.validate_core = function(values) { that.hide_error(); that.valid = true; if (IPA.is_empty(values)) { return that.valid; } for (var i=0; i