Permalink
Browse files

Auto-attaches onsubmit handler to trigger validation on form.

  • Loading branch information...
1 parent 83b93b7 commit b44df02e3bd27381ed4268a12a26c12c5dcee6fb @wailqill committed Sep 17, 2008
Showing with 23 additions and 4 deletions.
  1. +8 −2 src/wq.form_validator.js
  2. +15 −2 test/main_class_tests.html
View
@@ -13,10 +13,15 @@
this.elements = $A();
this.elements.add = addElements.curry(this);
+ this._submitHandler = this.validate.bindAsEventListener(this);
+ this.form.observe("submit", this._submitHandler);
this.form.fire("fv:create");
},
- validate: function() {
- return this.elements.invoke("validate").all();
+ validate: function(e) {
+ document.title = Math.random();
+ var result = this.elements.invoke("validate").all();
+ if (!result && e instanceof Event) e.stop();
+ return result;
}
});
function addElements(fv, elms, options) {
@@ -39,6 +44,7 @@
var fv = this.forElement(element);
if (!fv) return true;
var form = fv.form;
+ form.stopObserving("submit", fv._submitHandler);
delete fv;
form_validations.unset(form);
return true;
View
@@ -160,6 +160,19 @@ <h2>Main Class tests</h2>
}.bind(this));
wq.Form.Validation.Element.prototype.validate = f;
+ },
+ test_should_attach_validation_by_default: function() {
+ with_fv(function(fv) {
+ fv.elements.add("#person_email", { required: true });
+
+ var counter = 0;
+ var fCounter = function() { counter++; };
+ fv.form.observe("fv:element:validated", fCounter);
+ $("submit_button").click();
+ fv.form.stopObserving("fv:element:validated", fCounter);
+
+ this.assertEqual(1, counter);
+ }.bind(this));
}
/*
test_: function() {
@@ -179,7 +192,7 @@ <h2>Main Class tests</h2>
// ]]>
</script>
-<form action="index_submit" method="get" accept-charset="utf-8" id="my-form">
+<form action="main_class_tests.html" method="get" accept-charset="utf-8" id="my-form">
<p>
<label for="name">Name</label><input type="text" name="person[name]" value="" id="person_name">
</p>
@@ -196,7 +209,7 @@ <h2>Main Class tests</h2>
<label for="date">Date</label><input type="text" name="person[date]" value="" id="person_date">
</p>
- <p><input type="submit" value="Continue &rarr;"></p>
+ <p><input type="submit" value="Continue &rarr;" id="submit_button"></p>
</form>
</body>

0 comments on commit b44df02

Please sign in to comment.