Fixed evaluating checkbox dirty status

Problem:
When value in checkbox is modified twice in a row (so it is at its original value) an 'undo' button is still visible even when it shouldn't be.

Cause:
IPA server sends boolean values as 'TRUE' or 'FALSE' (strings). Checkbox_widget converts them to JavaScript? boolean (true, false). Save method in checkbox_widget is returning array with a boolean. So test_dirty method always evaluates to dirty because 'FALSE' != false.

This patch is fixing the problem.

https://fedorahosted.org/freeipa/ticket/2494
This commit is contained in:
Petr Vobornik 2012-03-09 15:52:05 +01:00
parent 51601ac794
commit 3ca0f6aee5
4 changed files with 24 additions and 18 deletions

View File

@ -487,6 +487,23 @@ IPA.checkbox_field = function(spec) {
var that = IPA.field(spec);
that.checked = spec.checked || false;
that.boolean_formatter = IPA.boolean_formatter();
that.load = function(record) {
that.record = record;
that.values = that.get_value(record, that.param);
var value = that.boolean_formatter.parse(that.values);
if (value === '') value = that.widget.checked; //default value
that.values = [value];
that.load_writable(record);
that.reset();
};
that.widgets_created = function() {
@ -510,13 +527,6 @@ IPA.checkboxes_field = function(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;
};

View File

@ -140,7 +140,7 @@ IPA.group_nonposix_checkbox_widget = function (spec) {
};
IPA.widget_factories['nonposix_checkbox'] = IPA.group_nonposix_checkbox_widget;
IPA.field_factories['nonposix_checkbox'] = IPA.checkbox_fields;
IPA.field_factories['nonposix_checkbox'] = IPA.checkbox_field;
IPA.group_adder_dialog = function(spec) {

View File

@ -223,7 +223,9 @@ test("Testing checkbox widget.", function() {
spec = {name:'title'};
base_widget_test('test_value');
var mock_record = { 'title': 'TRUE' };
//Changing mock record from 'TRUE' to true. Value normalization is field's
//job. Checkbox should work with booleans values.
var mock_record = { 'title': [true] };
widget.update(mock_record.title);
same(widget.save(),[true], "Checkbox is set");

View File

@ -632,18 +632,12 @@ IPA.checkbox_widget = function (spec) {
var value;
if (values && values.length) {
// use loaded value
value = values[0];
} else {
// use default value
value = that.checked;
}
// convert string into boolean
if (value === 'TRUE') {
value = true;
} else if (value === 'FALSE') {
value = false;
if (typeof value !== 'boolean') {
// use default value
value = that.checked;
}
that.input.attr('checked', value);