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:
parent
7ea6938de5
commit
cfc5baef61
4 changed files with 23 additions and 14 deletions
|
@ -51,7 +51,6 @@ Ox.Form = function(options, self) {
|
|||
},
|
||||
*/
|
||||
autovalidate: function(data) {
|
||||
data.valid = !!data.value.length;
|
||||
validate(i, data.valid);
|
||||
data.valid && self.$items[i].setMessage('');
|
||||
},
|
||||
|
@ -65,6 +64,7 @@ Ox.Form = function(options, self) {
|
|||
},
|
||||
*/
|
||||
change: function(data) {
|
||||
// fixme: shouldn't this be key/value instead of id/data?
|
||||
that.triggerEvent('change', {
|
||||
id: self.itemIds[i],
|
||||
data: data
|
||||
|
|
|
@ -36,10 +36,11 @@ Ox.FormElementGroup = function(options, self) {
|
|||
float: self.options.float // fixme: make this a class
|
||||
})
|
||||
.bindEvent({
|
||||
autovalidate: function(data) {
|
||||
that.triggerEvent({autovalidate: data});
|
||||
},
|
||||
validate: function(data) {
|
||||
that.triggerEvent({
|
||||
validate: data
|
||||
});
|
||||
that.triggerEvent({validate: data});
|
||||
}
|
||||
})
|
||||
.appendTo(that);
|
||||
|
|
|
@ -450,10 +450,13 @@ Ox.Input = function(options, self) {
|
|||
}
|
||||
|
||||
function autovalidateFunction(value) {
|
||||
var regexp = new RegExp(self.options.autovalidate);
|
||||
return value.split('').map(function(v) {
|
||||
return regexp.test(v) ? v : null;
|
||||
value = value.split('').map(function(v) {
|
||||
return self.options.autovalidate.test(v) ? v : null;
|
||||
}).join('');
|
||||
return {
|
||||
valid: !!value.length,
|
||||
value: value
|
||||
};
|
||||
}
|
||||
|
||||
function autovalidateTypeFunction(type, value) {
|
||||
|
@ -467,7 +470,6 @@ Ox.Input = function(options, self) {
|
|||
+ '$)'
|
||||
) : new RegExp('(^' + (self.options.min < 0 ? '\\-?' : '') + '\\d+)');
|
||||
if (type == 'float') {
|
||||
//Ox.print('--float--', value)
|
||||
if (value === '') {
|
||||
value = '0.' + self.decimals;
|
||||
cursor = [0, value.length];
|
||||
|
@ -484,7 +486,6 @@ Ox.Input = function(options, self) {
|
|||
value = '0' + value;
|
||||
cursor = [2, value.length];
|
||||
} else if (/\.$/.test(value)) {
|
||||
//Ox.print('$$$$$$$$$$$')
|
||||
value += self.decimals;
|
||||
cursor = [value.indexOf('.') + 1, value.length];
|
||||
} else if (/\./.test(value) && self.options.decimals) {
|
||||
|
@ -510,18 +511,23 @@ Ox.Input = function(options, self) {
|
|||
value = oldValue;
|
||||
cursor = oldCursor;
|
||||
}
|
||||
autovalidateCallback(value, cursor);
|
||||
autovalidateCallback({
|
||||
cursor: cursor,
|
||||
valid: true,
|
||||
value: value
|
||||
});
|
||||
}
|
||||
|
||||
function autovalidateCallback(newValue, newCursor) {
|
||||
function autovalidateCallback(data) {
|
||||
//Ox.print('autovalidateCallback', newValue, oldCursor)
|
||||
self.options.value = newValue;
|
||||
self.options.value = data.value;
|
||||
self.$input.val(self.options.value);
|
||||
!blur && cursor(
|
||||
newCursor || (oldCursor[1] + newValue.length - oldValue.length)
|
||||
data.cursor || (oldCursor[1] + data.value.length - oldValue.length)
|
||||
);
|
||||
that.triggerEvent('autovalidate', {
|
||||
value: self.options.value
|
||||
valid: data.valid,
|
||||
value: data.value
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -23,6 +23,8 @@ Ox.clean <f> Remove leading, trailing and double whitespace from a string
|
|||
"foo bar"
|
||||
> Ox.clean(" foo \n bar ")
|
||||
"foo\nbar"
|
||||
> Ox.clean(" foo\tbar ")
|
||||
"foo bar"
|
||||
@*/
|
||||
Ox.clean = function(str) {
|
||||
return Ox.map(str.split('\n'), function(str) {
|
||||
|
|
Loading…
Reference in a new issue