change autovalidate function for input elements so that it returns, along with the new value, a valid flag that indicates if the value is already valid

This commit is contained in:
rlx 2011-10-22 14:50:31 +00:00
parent 7ea6938de5
commit cfc5baef61
4 changed files with 23 additions and 14 deletions

View file

@ -51,7 +51,6 @@ Ox.Form = function(options, self) {
}, },
*/ */
autovalidate: function(data) { autovalidate: function(data) {
data.valid = !!data.value.length;
validate(i, data.valid); validate(i, data.valid);
data.valid && self.$items[i].setMessage(''); data.valid && self.$items[i].setMessage('');
}, },
@ -65,6 +64,7 @@ Ox.Form = function(options, self) {
}, },
*/ */
change: function(data) { change: function(data) {
// fixme: shouldn't this be key/value instead of id/data?
that.triggerEvent('change', { that.triggerEvent('change', {
id: self.itemIds[i], id: self.itemIds[i],
data: data data: data

View file

@ -36,10 +36,11 @@ Ox.FormElementGroup = function(options, self) {
float: self.options.float // fixme: make this a class float: self.options.float // fixme: make this a class
}) })
.bindEvent({ .bindEvent({
autovalidate: function(data) {
that.triggerEvent({autovalidate: data});
},
validate: function(data) { validate: function(data) {
that.triggerEvent({ that.triggerEvent({validate: data});
validate: data
});
} }
}) })
.appendTo(that); .appendTo(that);

View file

@ -450,10 +450,13 @@ Ox.Input = function(options, self) {
} }
function autovalidateFunction(value) { function autovalidateFunction(value) {
var regexp = new RegExp(self.options.autovalidate); value = value.split('').map(function(v) {
return value.split('').map(function(v) { return self.options.autovalidate.test(v) ? v : null;
return regexp.test(v) ? v : null;
}).join(''); }).join('');
return {
valid: !!value.length,
value: value
};
} }
function autovalidateTypeFunction(type, value) { function autovalidateTypeFunction(type, value) {
@ -467,7 +470,6 @@ Ox.Input = function(options, self) {
+ '$)' + '$)'
) : new RegExp('(^' + (self.options.min < 0 ? '\\-?' : '') + '\\d+)'); ) : new RegExp('(^' + (self.options.min < 0 ? '\\-?' : '') + '\\d+)');
if (type == 'float') { if (type == 'float') {
//Ox.print('--float--', value)
if (value === '') { if (value === '') {
value = '0.' + self.decimals; value = '0.' + self.decimals;
cursor = [0, value.length]; cursor = [0, value.length];
@ -484,7 +486,6 @@ Ox.Input = function(options, self) {
value = '0' + value; value = '0' + value;
cursor = [2, value.length]; cursor = [2, value.length];
} else if (/\.$/.test(value)) { } else if (/\.$/.test(value)) {
//Ox.print('$$$$$$$$$$$')
value += self.decimals; value += self.decimals;
cursor = [value.indexOf('.') + 1, value.length]; cursor = [value.indexOf('.') + 1, value.length];
} else if (/\./.test(value) && self.options.decimals) { } else if (/\./.test(value) && self.options.decimals) {
@ -510,18 +511,23 @@ Ox.Input = function(options, self) {
value = oldValue; value = oldValue;
cursor = oldCursor; cursor = oldCursor;
} }
autovalidateCallback(value, cursor); autovalidateCallback({
cursor: cursor,
valid: true,
value: value
});
} }
function autovalidateCallback(newValue, newCursor) { function autovalidateCallback(data) {
//Ox.print('autovalidateCallback', newValue, oldCursor) //Ox.print('autovalidateCallback', newValue, oldCursor)
self.options.value = newValue; self.options.value = data.value;
self.$input.val(self.options.value); self.$input.val(self.options.value);
!blur && cursor( !blur && cursor(
newCursor || (oldCursor[1] + newValue.length - oldValue.length) data.cursor || (oldCursor[1] + data.value.length - oldValue.length)
); );
that.triggerEvent('autovalidate', { that.triggerEvent('autovalidate', {
value: self.options.value valid: data.valid,
value: data.value
}); });
} }

View file

@ -23,6 +23,8 @@ Ox.clean <f> Remove leading, trailing and double whitespace from a string
"foo bar" "foo bar"
> Ox.clean(" foo \n bar ") > Ox.clean(" foo \n bar ")
"foo\nbar" "foo\nbar"
> Ox.clean(" foo\tbar ")
"foo bar"
@*/ @*/
Ox.clean = function(str) { Ox.clean = function(str) {
return Ox.map(str.split('\n'), function(str) { return Ox.map(str.split('\n'), function(str) {