validate onSubmit

This commit is contained in:
elclanrs 2013-10-09 08:07:26 -04:00
parent 3d7df77ae2
commit c69c0f056b
5 changed files with 43 additions and 45 deletions

View file

@ -35,6 +35,14 @@
font-size: 130%; font-size: 130%;
font-weight: bold; font-weight: bold;
} }
.idealforms.adaptive #invalid {
margin-left: 0 !important;
}
.idealforms.adaptive .field.buttons label {
height: 0;
}
</style> </style>
</head> </head>
<body> <body>
@ -129,7 +137,7 @@
<div class="field buttons"> <div class="field buttons">
<label class="main">&nbsp;</label> <label class="main">&nbsp;</label>
<button type="button" class="next">&laquo; Previous</button> <button type="button" class="prev">&laquo; Prev</button>
<button type="button" class="next">Next &raquo;</button> <button type="button" class="next">Next &raquo;</button>
</div> </div>
@ -171,7 +179,7 @@
<div class="field buttons"> <div class="field buttons">
<label class="main">&nbsp;</label> <label class="main">&nbsp;</label>
<button type="button" class="next">&laquo; Previous</button> <button type="button" class="prev">&laquo; Prev</button>
<button type="submit" class="submit">Submit</button> <button type="submit" class="submit">Submit</button>
</div> </div>

View file

@ -55,13 +55,6 @@ module.exports = {
$.idealforms._requests[input.name].abort(); $.idealforms._requests[input.name].abort();
this._getField(input).removeClass('ajax'); this._getField(input).removeClass('ajax');
} }
},
_validateAll: function(input) {
if ($(input).data('idealforms-ajax') && this._getField(input).data('idealforms-valid') === true) {
$.idealforms._requests[input.name].abort();
this._getField(input).removeClass('ajax');
}
} }
} }

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -13,16 +13,17 @@ module.exports = {
this.$fields = $(); this.$fields = $();
this.$inputs = $(); this.$inputs = $();
this.$form.submit(function(e) {
e.preventDefault();
self.focusFirstInvalid();
self.opts.onSubmit.call(self, self.getInvalid().length, e);
});
this._inject('_init'); this._inject('_init');
this.addRules(this.opts.rules || {}); this.addRules(this.opts.rules || {});
this.$form.submit(function(e) {
e.preventDefault();
self._validateAll();
self.focusFirstInvalid();
self.opts.onSubmit.call(self, self.getInvalid().length, e);
});
if (! this.opts.silentLoad) this.focusFirstInvalid(); if (! this.opts.silentLoad) this.focusFirstInvalid();
}, },
@ -55,14 +56,7 @@ module.exports = {
$(input) $(input)
.on('change keyup', function(e) { .on('change keyup', function(e) {
var oldValue = $field.data('idealforms-value');
if (e.which == 9 || e.which == 16) return; if (e.which == 9 || e.which == 16) return;
if (! $(this).is(':checkbox, :radio') && oldValue == this.value) return;
$field.data('idealforms-value', this.value);
self._validate(this, true, true); self._validate(this, true, true);
}) })
.focus(function() { .focus(function() {
@ -125,9 +119,17 @@ module.exports = {
var self = this var self = this
, $field = this._getField(input) , $field = this._getField(input)
, userRules = this.opts.rules[input.name].split($.idealforms.ruleSeparator) , userRules = this.opts.rules[input.name].split($.idealforms.ruleSeparator)
, oldValue = $field.data('idealforms-value')
, valid = true , valid = true
, rule; , rule;
// Don't validate input if value hasn't changed
if (! $(input).is(':checkbox, :radio') && oldValue == input.value) {
return $field.data('idealforms-valid');
}
$field.data('idealforms-value', input.value);
// Non-required input with empty value must pass validation // Non-required input with empty value must pass validation
if (! input.value && ! this._isRequired(input)) { if (! input.value && ! this._isRequired(input)) {
$field.removeData('idealforms-valid'); $field.removeData('idealforms-valid');