jq-idealform-test/js/public.js
2013-10-05 22:10:30 -04:00

68 lines
1.5 KiB
JavaScript

/**
* Public methods
*/
module.exports = {
addRules: function(rules) {
var self = this;
var $inputs = this.$form.find($.map(rules, function(_, name) {
return '[name="'+ name +'"]';
}).join(','));
$.extend(this.opts.rules, rules);
$inputs.each(function(){ self._buildField(this) });
this.$inputs = this.$inputs
.add($inputs)
.each(function(){ self._validate(this, true) });
this.$fields.find(this.opts.error).hide();
this._inject('addRules');
},
getInvalid: function() {
return this.$fields.filter(function() {
return $(this).data('idealforms-valid') === false;
});
},
focusFirstInvalid: function() {
var firstInvalid = this._getFirstInvalid()[0];
if (firstInvalid) {
this._handleError(firstInvalid);
this._handleStyle(firstInvalid);
this._inject('focusFirstInvalid', firstInvalid);
firstInvalid.focus();
}
},
isValid: function(name) {
if (name) return ! this.getInvalid().find('[name="'+ name +'"]').length;
return ! this.getInvalid().length;
},
reset: function(name) {
var self = this
, $inputs = this.$inputs;
if (name) $inputs = $inputs.filter('[name="'+ name +'"]');
$inputs.filter('input:not(:checkbox, :radio)').val('');
$inputs.filter(':checkbox, :radio').prop('checked', false);
$inputs.filter('select').find('option').prop('selected', function() {
return this.defaultSelected;
});
$inputs.change().each(function(){ self._resetErrorAndStyle(this) });
this._inject('reset', name);
}
};