Commit 44ac8a54 authored by Aron Carroll's avatar Aron Carroll

More improvements to the form validation

 * Validate on "change" rather than submit. This prevents errors
   appearing when the user tabs in/out of an input.
 * Keypress updating of errors in place errors to immediately indicate
   to the user when they have fixed an error.
parent bce25d56
formErrorList = -> formField = ->
link: (scope, elem, attr, form) -> link: (scope, elem, attr, form) ->
field = form[attr.target] field = form[attr.target]
return unless field? return unless field?
field.$setUntouched = -> field.unsetFieldError = ->
field.$untouched = true elem.removeClass('form-field-error')
field.$touched = false
elem.parent().children('input').removeClass('ng-touched') field.setFieldError = ->
elem.addClass('form-field-error')
field.$setTouched = ->
field.$untouched = false
field.$touched = true
elem.parent().children('input').addClass('ng-touched')
require: '^form' require: '^form'
restrict: 'C' restrict: 'C'
formValidate = -> formValidate = ->
link: (scope, elem, attr, form) -> link: (scope, elem, attr, form) ->
elem.on 'blur', ':input', -> updateField = (field) ->
form[this.name]?.$setTouched() return unless field?
if field.$valid
field.unsetFieldError()
else
field.setFieldError()
# Immediately show feedback for corrections.
elem.on 'keyup', ':input', ->
updateField(form[this.name]) if form[this.name]?.$valid
# Validate field when the content changes.
elem.on 'change', ':input', ->
updateField(form[this.name])
# Validate the field when submit is clicked.
elem.on 'submit', (event) -> elem.on 'submit', (event) ->
for fieldName of form updateField(field) for _, field of form
form[fieldName]?.$setTouched?()
require: 'form' require: 'form'
...@@ -444,7 +455,7 @@ whenscrolled = ['$window', ($window) -> ...@@ -444,7 +455,7 @@ whenscrolled = ['$window', ($window) ->
angular.module('h.directives', ['ngSanitize']) angular.module('h.directives', ['ngSanitize'])
.directive('formErrorList', formErrorList) .directive('formField', formField)
.directive('formValidate', formValidate) .directive('formValidate', formValidate)
.directive('fuzzytime', fuzzytime) .directive('fuzzytime', fuzzytime)
.directive('markdown', markdown) .directive('markdown', markdown)
......
...@@ -18,8 +18,14 @@ ...@@ -18,8 +18,14 @@
} }
} }
.form-field-error .form-input { .form-field-error {
.form-input {
@extend %form-input-error-state; @extend %form-input-error-state;
}
.form-error-list {
display: block;
}
} }
.form-input, .form-input,
...@@ -71,15 +77,6 @@ ...@@ -71,15 +77,6 @@
color: #cbbb95; color: #cbbb95;
} }
} }
// This is not ideal
&.ng-dirty.ng-invalid {
@extend %form-input-error-state;
&.ng-touched ~ .form-error-list {
display: block;
}
}
} }
.form-select { .form-select {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment