Updated Branches: refs/heads/5.4-js-rewrite 0e4027a25 -> 658142934
Event handlers that detect a validation error should return false to prevent the event from bubbling Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/65814293 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/65814293 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/65814293 Branch: refs/heads/5.4-js-rewrite Commit: 658142934fd7d12857c58589c71aeccde7fae372 Parents: 12d4d53 Author: Howard M. Lewis Ship <[email protected]> Authored: Fri Nov 2 15:08:34 2012 -0700 Committer: Howard M. Lewis Ship <[email protected]> Committed: Fri Nov 2 15:08:34 2012 -0700 ---------------------------------------------------------------------- .../META-INF/modules/core/events.coffee | 5 +++++ .../META-INF/modules/core/validation.coffee | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/65814293/tapestry-core/src/main/coffeescript/META-INF/modules/core/events.coffee ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/events.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/events.coffee index 7286bfb..2f3d3d8 100644 --- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/events.coffee +++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/events.coffee @@ -40,6 +40,10 @@ define # # A field that is blank but not required is considered valid: the translate and validate steps are skipped. # + # When a validation error occurs, the event handler should present the validation error (see below), but also + # return `false`. This will prevent the event from propogating to other event handlers (Tapestry only supports + # a single validation exception per field). + # # Presenting validation error: The event handler has two options for indicating a validation failure # at any of the three steps: # @@ -47,6 +51,7 @@ define # make the validation error visible) # * set the `error` property of the memo to the message to display; this will indicate a failure, and the # `showValidationError` event will be triggered automatically. + # * In addition, return `false` to prevent the event bubbling (see note above). field: # Perform the optionality check. The event memo includes a `value` property. If the field is required http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/65814293/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee index 2783e3c..7440721 100644 --- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee +++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee @@ -94,11 +94,12 @@ define ["_", "core/spi", "core/events", "core/utils", "core/messages", "core/fie memo.translated = result catch e memo.error = (field.attribute "data-translation-message") or e.message or "ERROR" + return false spi.onDocument events.field.optional, "[data-optionality=required]", (event, memo) -> if utils.isBlank memo.value - memo.error = (this.attribute "data-required-message") || "REQUIRED" + memo.error = (this.attribute "data-required-message") or "REQUIRED" spi.onDocument events.field.translate, "[data-translation=numeric]", (event, memo) -> translate this, memo, false @@ -111,24 +112,28 @@ define ["_", "core/spi", "core/events", "core/utils", "core/messages", "core/fie if memo.translated.length < min memo.error = (this.attribute "data-min-length-message") or "TOO SHORT" + return false spi.onDocument events.field.validate, "[data-validate-max-length]", (event, memo) -> - min = parseInt this.attribute "data-validate-max-length" + max = parseInt this.attribute "data-validate-max-length" - if memo.translated.length > min + if memo.translated.length > max memo.error = (this.attribute "data-max-length-message") or "TOO LONG" + return false spi.onDocument events.field.validate, "[data-validate-max]", (event, memo) -> max = parseInt this.attribute "data-validate-max" if memo.translated > max memo.error = (this.attribute "data-max-message") or "TOO LARGE" + return false spi.onDocument events.field.validate, "[data-validate-min]", (event, memo) -> min = parseInt this.attribute "data-validate-min" if memo.translated < min memo.error = (this.attribute "data-min-message") or "TOO SMALL" + return false spi.onDocument events.field.validate, "[data-validate-regexp]", (event, memo) -> @@ -140,6 +145,7 @@ define ["_", "core/spi", "core/events", "core/utils", "core/messages", "core/fie unless re.test memo.translated memo.error = (this.attribute "data-regexp-message") or "INVALID" + return false # Export the number parser, just to be nice (and to support some testing). return { parseNumber } \ No newline at end of file
