|
|
@@ -389,7 +389,7 @@
|
|
|
default:
|
|
|
$field.off(events).on(events, function() {
|
|
|
if (that._exceedThreshold($(this))) {
|
|
|
- that.validateFieldElement($(this));
|
|
|
+ that.validateField($(this));
|
|
|
}
|
|
|
});
|
|
|
break;
|
|
|
@@ -519,7 +519,7 @@
|
|
|
for (var i = 0; i < fields.length; i++) {
|
|
|
$(fields[i]).off(events).on(events, function() {
|
|
|
if (that._exceedThreshold($(this))) {
|
|
|
- that.validateFieldElement($(this));
|
|
|
+ that.validateField($(this));
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@@ -662,76 +662,77 @@
|
|
|
* @returns {BootstrapValidator}
|
|
|
*/
|
|
|
validateField: function(field) {
|
|
|
- var fields = this.getFieldElements(field),
|
|
|
- type = fields.attr('type'),
|
|
|
- n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
|
|
|
-
|
|
|
- for (var i = 0; i < n; i++) {
|
|
|
- this.validateFieldElement($(fields[i]));
|
|
|
+ var fields = $([]), type;
|
|
|
+ switch (typeof field) {
|
|
|
+ case 'object':
|
|
|
+ fields = field;
|
|
|
+ field = field.attr('data-bv-field');
|
|
|
+ break;
|
|
|
+ case 'string':
|
|
|
+ fields = this.getFieldElements(field);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
- return this;
|
|
|
- },
|
|
|
+ if (this.options.fields[field]['enabled'] == false) {
|
|
|
+ return this;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Validate field element
|
|
|
- *
|
|
|
- * @param {jQuery} $field The field element
|
|
|
- * @returns {BootstrapValidator}
|
|
|
- */
|
|
|
- validateFieldElement: function($field) {
|
|
|
var that = this,
|
|
|
- field = $field.attr('data-bv-field'),
|
|
|
- fields = this.getFieldElements(field),
|
|
|
- type = $field.attr('type'),
|
|
|
- updateAll = (fields && fields.length == 1) || ('radio' == type) || ('checkbox' == type),
|
|
|
+ type = fields.attr('type'),
|
|
|
+ total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length,
|
|
|
+ updateAll = ('radio' == type || 'checkbox' == type),
|
|
|
validators = this.options.fields[field].validators,
|
|
|
validatorName,
|
|
|
validateResult;
|
|
|
|
|
|
- if (this.options.fields[field]['enabled'] == false || this._isExcluded($field)) {
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- for (validatorName in validators) {
|
|
|
- if ($field.data('bv.dfs.' + validatorName)) {
|
|
|
- $field.data('bv.dfs.' + validatorName).reject();
|
|
|
- }
|
|
|
-
|
|
|
- // Don't validate field if it is already done
|
|
|
- var result = $field.data('bv.result.' + validatorName);
|
|
|
- if (result == this.STATUS_VALID || result == this.STATUS_INVALID) {
|
|
|
- this._onValidateFieldCompleted($field);
|
|
|
+ for (var i = 0; i < total; i++) {
|
|
|
+ var $field = $(fields[i]);
|
|
|
+ if (this._isExcluded($field)) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- $field.data('bv.result.' + validatorName, this.STATUS_VALIDATING);
|
|
|
- validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
|
|
|
+ for (validatorName in validators) {
|
|
|
+ if ($field.data('bv.dfs.' + validatorName)) {
|
|
|
+ $field.data('bv.dfs.' + validatorName).reject();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Don't validate field if it is already done
|
|
|
+ var result = $field.data('bv.result.' + validatorName);
|
|
|
+ if (result == this.STATUS_VALID || result == this.STATUS_INVALID) {
|
|
|
+ this._onValidateFieldCompleted($field);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- // validateResult can be a $.Deferred object ...
|
|
|
- if ('object' == typeof validateResult) {
|
|
|
- this.updateStatus(updateAll ? field : $field, this.STATUS_VALIDATING, validatorName);
|
|
|
- $field.data('bv.dfs.' + validatorName, validateResult);
|
|
|
+ $field.data('bv.result.' + validatorName, this.STATUS_VALIDATING);
|
|
|
+ validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
|
|
|
|
|
|
- validateResult.done(function($f, v, isValid, message) {
|
|
|
- // v is validator name
|
|
|
- $f.removeData('bv.dfs.' + v);
|
|
|
- if (message) {
|
|
|
- // Update the error message
|
|
|
- $field.data('bv.messages').find('.help-block[data-bv-validator="' + v + '"][data-bv-for="' + $f.attr('data-bv-field') + '"]').html(message);
|
|
|
- }
|
|
|
+ // validateResult can be a $.Deferred object ...
|
|
|
+ if ('object' == typeof validateResult) {
|
|
|
+ this.updateStatus(updateAll ? field : $field, this.STATUS_VALIDATING, validatorName);
|
|
|
+ $field.data('bv.dfs.' + validatorName, validateResult);
|
|
|
+
|
|
|
+ validateResult.done(function($f, v, isValid, message) {
|
|
|
+ // v is validator name
|
|
|
+ $f.removeData('bv.dfs.' + v);
|
|
|
+ if (message) {
|
|
|
+ // Update the error message
|
|
|
+ $field.data('bv.messages').find('.help-block[data-bv-validator="' + v + '"][data-bv-for="' + $f.attr('data-bv-field') + '"]').html(message);
|
|
|
+ }
|
|
|
|
|
|
- that.updateStatus(updateAll ? $f.attr('data-bv-field') : $f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
|
|
|
+ that.updateStatus(updateAll ? $f.attr('data-bv-field') : $f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
|
|
|
|
|
|
- if (isValid && that._submitIfValid == true) {
|
|
|
- // If a remote validator returns true and the form is ready to submit, then do it
|
|
|
- that._submit();
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- // ... or a boolean value
|
|
|
- else if ('boolean' == typeof validateResult) {
|
|
|
- this.updateStatus(updateAll ? field : $field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
|
|
|
+ if (isValid && that._submitIfValid == true) {
|
|
|
+ // If a remote validator returns true and the form is ready to submit, then do it
|
|
|
+ that._submit();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ // ... or a boolean value
|
|
|
+ else if ('boolean' == typeof validateResult) {
|
|
|
+ this.updateStatus(updateAll ? field : $field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -739,7 +740,7 @@
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
- * Update all validating results of elements which have the same field name
|
|
|
+ * Update all validating results of field
|
|
|
*
|
|
|
* @param {String|jQuery} field The field name or field element
|
|
|
* @param {String} status The status. Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
|
|
|
@@ -760,11 +761,11 @@
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- var that = this,
|
|
|
- type = fields.attr('type'),
|
|
|
- n = ('radio' == type || 'checkbox' == type) ? 1 : fields.length;
|
|
|
+ var that = this,
|
|
|
+ type = fields.attr('type'),
|
|
|
+ total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length;
|
|
|
|
|
|
- for (var i = 0; i < n; i++) {
|
|
|
+ for (var i = 0; i < total; i++) {
|
|
|
var $field = $(fields[i]),
|
|
|
$parent = $field.parents('.form-group'),
|
|
|
$message = $field.data('bv.messages'),
|
|
|
@@ -911,10 +912,10 @@
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- var type = fields.attr('type'),
|
|
|
- n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length,
|
|
|
+ var type = fields.attr('type'),
|
|
|
+ total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length,
|
|
|
$field, validatorName, status;
|
|
|
- for (var i = 0; i < n; i++) {
|
|
|
+ for (var i = 0; i < total; i++) {
|
|
|
$field = $(fields[i]);
|
|
|
if (this._isExcluded($field)) {
|
|
|
continue;
|
|
|
@@ -1135,6 +1136,7 @@
|
|
|
}
|
|
|
|
|
|
// Update the cache
|
|
|
+ $field.remove();
|
|
|
delete this._cacheFields[field];
|
|
|
if ('checkbox' == type || 'radio' == type) {
|
|
|
this._initField(field);
|