You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2014/03/25 00:39:40 UTC
git commit: TAP5-2307: Focus on field with error when validating
Repository: tapestry-5
Updated Branches:
refs/heads/master 8e1c5fdc9 -> cb2ef6a24
TAP5-2307: Focus on field with error when validating
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/cb2ef6a2
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/cb2ef6a2
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/cb2ef6a2
Branch: refs/heads/master
Commit: cb2ef6a24f2e22c3646d046686193d463147a28c
Parents: 8e1c5fd
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Mon Mar 24 16:39:32 2014 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Mon Mar 24 16:39:32 2014 -0700
----------------------------------------------------------------------
.../META-INF/modules/t5/core/events.coffee | 9 +++++----
.../META-INF/modules/t5/core/forms.coffee | 20 +++++++++++++++++---
2 files changed, 22 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/cb2ef6a2/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/events.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/events.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/events.coffee
index e705d0d..95af3ab 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/events.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/events.coffee
@@ -1,4 +1,4 @@
-# Copyright 2012, 2013 The Apache Software Foundation
+# Copyright 2012-2014 The Apache Software Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -23,7 +23,8 @@ define
# All events are triggered on a specific HTML `<form>` element, and top-level handlers take it from there.
form:
# Triggered after fields have been validated, when there are no field validation exceptions, to allow for
- # cross-form validation.
+ # cross-form validation. Passed a memo object: the handler should set the `error` property of the memo
+ # to true to indicate a validation exception occured, and the form submission should be prevented.
validate: "t5:form:validate"
# Triggered after `validate` (when there are no prior validation exceptions), to allow certain elements
@@ -81,7 +82,7 @@ define
# interested in participating with user input validation). The default implementation fires a series of
# events: `optional`, `translate`, `validate`. The latter two are always skipped if the input is blank, or if
# a preceding event set the memo's `error` property to true. If all events complete without setting an error,
- # then the `clearValidationError` event is triggered, so remove any validation errors from previous
+ # then the `clearValidationError` event is triggered, to remove any validation errors from previous
# validation cycles.
#
# This event is passed a memo object; it should set the memo's `error` property to true if validation failed
@@ -89,7 +90,7 @@ define
inputValidation: "t5:field:input-validation"
# Clears and hides the element used to display validation error messages. There is no memo for
- # this event. The p.help-block for the field is located (if it exists) and empties and hidden.
+ # this event. The p.help-block for the field is located (if it exists) and emptied and hidden.
# The containing .form-group element (if it exists) has its "has-error" class name removed.
clearValidationError: "t5:field:clear-validation-error"
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/cb2ef6a2/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/forms.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/forms.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/forms.coffee
index dca7cf6..51c0e03 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/forms.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/t5/core/forms.coffee
@@ -113,20 +113,34 @@ define ["./events", "./dom", "underscore"],
@meta SKIP_VALIDATION, null
- memo = error: false
+ hasError = false
+ focusField = null
for field in @find "[data-validation]"
+ memo = {}
where = -> "triggering #{events.field.inputValidation} event on #{field.toString()}"
field.trigger events.field.inputValidation, memo
+ if memo.error
+ hasError = true
+ focusField = field unless focusField
+
# Only do form validation if all individual field validation
# was successful.
- unless memo.error
+ unless hasError
+ memo = {}
where = -> "trigging cross-form validation event"
@trigger events.form.validate, memo
- if memo.error
+ hasError = memo.error
+
+ if hasError
clearSubmittingHidden this
+
+ # If a specific field has been identified as the source of the validation error, then
+ # focus on it.
+ focusField.focus() if focusField
+
# Cancel the original submit event when there's an error
return false