From c1d97026087f478257b176362aa70a20f7baaf8b Mon Sep 17 00:00:00 2001 From: Wilker Lucio Date: Wed, 25 Aug 2010 02:10:25 -0300 Subject: [PATCH] automatic form parsing --- examples/auto_parse.html | 26 +++++++++++++++++++ lib/jcheck.form.js | 30 ++++++++++++++++++++-- spec/fixtures/form_inline_validations.html | 13 ++++++++++ spec/unit/form_spec.js | 30 ++++++++++++++++++++++ src/jcheck.form.coffee | 23 +++++++++++++++++ 5 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 examples/auto_parse.html create mode 100644 spec/fixtures/form_inline_validations.html diff --git a/examples/auto_parse.html b/examples/auto_parse.html new file mode 100644 index 0000000..15903d0 --- /dev/null +++ b/examples/auto_parse.html @@ -0,0 +1,26 @@ + + + + + jCheck + + + + + + + + + + + +

Post your comment

+
+
+ + +
+ +
+ + \ No newline at end of file diff --git a/lib/jcheck.form.js b/lib/jcheck.form.js index 4a32bef..a89c8cd 100644 --- a/lib/jcheck.form.js +++ b/lib/jcheck.form.js @@ -9,15 +9,38 @@ var __bind = function(func, context) { field_prefix: null, notifiers: $.FormCheck.default_notifiers, live_notifiers: $.FormCheck.default_live_notifiers, - language: $.FormCheck.default_locale + language: $.FormCheck.default_locale, + auto_parse: true }, options || {}); this.field_cache = {}; this.errors = new $.FormCheck.Errors(this); this.validations = []; this.hook_events(); this.setup_notifiers(); + if (this.options.auto_parse) { + this.parse_inline_validations(); + } return this; }; + $.FormCheck.prototype.parse_inline_validations = function() { + return this.form.find(":input[data-validations]").each(__bind(function(index, element) { + var e, field_name, validations; + e = $(element); + field_name = this.parse_field_name(e); + validations = this.parse_validation_string(e.attr("data-validations")); + return this.validates(field_name, validations); + }, this)); + }; + $.FormCheck.prototype.parse_validation_string = function(validation_string) { + var validations; + validations = {}; + try { + validations = eval(("({" + (validation_string) + "})")); + } catch (e) { + console.error(("can't parse \"" + (validation_string) + "\"")); + } + return validations; + }; $.FormCheck.prototype.hook_events = function() { var self; this.form.submit(__bind(function(e) { @@ -242,7 +265,10 @@ var __bind = function(func, context) { label_element = this.form_checker.form.find(("label[for='" + (field_id) + "']")); return label_element.length > 0 ? label_element.text() : this.field_name; }; - return ($.fn.jcheck = function(options) { + $.fn.jcheck = function(options) { return new $.FormCheck($(this), options || {}); + }; + return $(function() { + return $("form[data-jcheck=true]").jcheck(); }); })(jQuery); \ No newline at end of file diff --git a/spec/fixtures/form_inline_validations.html b/spec/fixtures/form_inline_validations.html new file mode 100644 index 0000000..6131e4d --- /dev/null +++ b/spec/fixtures/form_inline_validations.html @@ -0,0 +1,13 @@ +
+
+ + +
+
+ + +
+
+ +
+
\ No newline at end of file diff --git a/spec/unit/form_spec.js b/spec/unit/form_spec.js index 953519d..22ee715 100644 --- a/spec/unit/form_spec.js +++ b/spec/unit/form_spec.js @@ -153,6 +153,36 @@ describe "jCheck" v.field("multiple_select").value().should.eql ["2", "4", "5"] end end + + describe "auto-parse" + describe "parsing validation strings" + it "should create the object with validations" + v = $("
").jcheck() + v.parse_validation_string("presence: true, format: /\\d+/").should.eql {presence: true, format: /\d+/} + end + + it "should return log error and return blank object if string is valid" + stub(console, 'error') + v = $("
").jcheck() + console.should.receive('error').with_args(an_instance_of(String)) + v.parse_validation_string("presence: namaste").should.eql {} + end + end + + it "should parse fields with data-validation attribute" + v = $(fixture("form_inline_validations")).jcheck() + + v.should.have_error_message_on("can't be blank", "text") + v.should.have_error_message_on("can't be blank", "email") + v.should.have_error_message_on("is invalid", "email") + end + + it "should not parse fields if auto_parse option is sent as false" + v = $(fixture("form_inline_validations")).jcheck({auto_parse: false}) + v.is_valid() + v.errors.size().should.be 0 + end + end end end end \ No newline at end of file diff --git a/src/jcheck.form.coffee b/src/jcheck.form.coffee index 88f8097..014657a 100644 --- a/src/jcheck.form.coffee +++ b/src/jcheck.form.coffee @@ -22,6 +22,7 @@ notifiers: $.FormCheck.default_notifiers live_notifiers: $.FormCheck.default_live_notifiers language: $.FormCheck.default_locale + auto_parse: true }, options || {}) @field_cache = {} @@ -29,6 +30,24 @@ @validations = [] @hook_events() @setup_notifiers() + @parse_inline_validations() if @options.auto_parse + + parse_inline_validations: -> + @form.find(":input[data-validations]").each (index, element) => + e = $(element) + field_name = @parse_field_name(e) + validations = @parse_validation_string(e.attr("data-validations")) + @validates(field_name, validations) + + parse_validation_string: (validation_string) -> + validations = {} + + try + validations = eval("({#{validation_string}})") + catch e + console.error("can't parse \"#{validation_string}\"") + + validations hook_events: -> @form.submit (e) => @@ -209,4 +228,8 @@ # jquery hook $.fn.jcheck = (options) -> new $.FormCheck($(this), options || {}) + + # auto_parse + $ -> + $("form[data-jcheck=true]").jcheck() )(jQuery)