Commit c8223fe2 authored by Aron Carroll's avatar Aron Carroll

Finish form validation implementation

parent 35cf003f
...@@ -712,23 +712,34 @@ class Auth ...@@ -712,23 +712,34 @@ class Auth
_timeout = null _timeout = null
_reset = -> _reset = ->
delete $scope.errors
angular.extend $scope.model, base angular.extend $scope.model, base
for own _, ctrl of $scope when angular.isFunction ctrl?.$setPristine for own _, ctrl of $scope when angular.isFunction ctrl?.$setPristine
ctrl.$setPristine() ctrl.$setPristine()
for own _, field of ctrl when angular.isFunction field?.$setUntouched
field.$setUntouched() _updateFormValidity = (form, reason) ->
if reason == 'Invalid username or password.'
form.password.$setValidity('invalid', false)
else if reason
form.responseErrorMessage = reason
_updateFieldValidity = (form, errors) ->
for field, error of errors
form[field].$setValidity('response', false)
form[field].responseErrorMessage = error
_resetFormValidity = (form) ->
form.password.$setValidity('invalid', true)
for own _, field of form when field.$setValidity
field.$setValidity('response', true)
field.responseErrorMessage = null
_error = (form, data) -> _error = (form, data) ->
{errors, reason} = data {errors, reason} = data
if reason # Seems legit, and only used for login messages... _updateFormValidity(form, reason)
$scope.login.password.$setValidity('server', false) _updateFieldValidity(form, errors)
$scope.login.password.serverError = reason
for field, error of errors $scope.$emit('error', form.$name)
$scope[form][field].$setValidity('server', false)
$scope[form][field].serverError = error
_startTimeout = -> _startTimeout = ->
# Reset the auth forms after five minutes of inactivity # Reset the auth forms after five minutes of inactivity
...@@ -743,13 +754,15 @@ class Auth ...@@ -743,13 +754,15 @@ class Auth
_startTimeout() _startTimeout()
$scope.submit = (form) -> $scope.submit = (form) ->
_resetFormValidity(form)
angular.extend session, $scope.model angular.extend session, $scope.model
return unless form.$valid return unless form.$valid
promise = session["$#{form.$name}"] -> promise = session["$#{form.$name}"] ->
$scope.$emit 'success', form.$name $scope.$emit('success', form.$name)
promise.then(_reset, _error.bind(null, form.$name)) promise.then(_reset, _error.bind(null, form))
class Editor class Editor
......
formField = -> formField = ->
link: (scope, elem, attr, form) -> link: (scope, elem, attr, form) ->
field = form[attr.target] field = form[attr.target]
errorClass = attr.errorClass
return unless field? return unless field?
unless errorClass
throw new Error('FormField directive requires an error-class attribute')
field.unsetFieldError = -> field.unsetFieldError = ->
elem.removeClass('form-field-error') elem.removeClass(errorClass)
field.setFieldError = -> field.setFieldError = ->
elem.addClass('form-field-error') elem.addClass(errorClass)
require: '^form' require: '^form'
restrict: 'C' restrict: 'C'
...@@ -33,7 +38,12 @@ formValidate = -> ...@@ -33,7 +38,12 @@ formValidate = ->
# Validate the field when submit is clicked. # Validate the field when submit is clicked.
elem.on 'submit', (event) -> elem.on 'submit', (event) ->
updateField(field) for own key, field of form when key[0] != '$' updateField(field) for own _, field of form when field.$name?
# Validate when a response is processed.
scope.$on 'error', (event, name) ->
return unless form.$name == name
updateField(field) for own _, field of form when field.$name?
require: 'form' require: 'form'
......
...@@ -6,6 +6,10 @@ ...@@ -6,6 +6,10 @@
margin-bottom: 10px; margin-bottom: 10px;
} }
.form-description {
margin-bottom: 1em;
}
%form-input-error-state { %form-input-error-state {
&, &:focus, &.js-focus { &, &:focus, &.js-focus {
color: #b4777a; color: #b4777a;
......
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