Commit dab18b67 authored by Randall Leeds's avatar Randall Leeds

Move form-wide response error code to formHelpers

Add an (optional) third argument to the method
`formHelpers#applyValidationErrors()` that can take an error string,
``reason``, to set on the form itself, rather than any particular
field. Remove the duplicate code in the auth directive to reset
the response errors on each field, which is handled already in the
``form-input`` directive.
parent 9a8ffaaf
...@@ -15,19 +15,12 @@ class AuthController ...@@ -15,19 +15,12 @@ class AuthController
failure = (form, response) -> failure = (form, response) ->
{errors, reason} = response.data {errors, reason} = response.data
formHelpers.applyValidationErrors(form, errors, reason)
if reason
if reason == 'Invalid username or password.'
form.password.$setValidity('response', false)
form.password.responseErrorMessage = reason
else
form.responseErrorMessage = reason
else
form.responseErrorMessage = null
formHelpers.applyValidationErrors(form, errors)
this.submit = (form) -> this.submit = (form) ->
delete form.responseErrorMessage
form.$setValidity('response', true)
return unless form.$valid return unless form.$valid
data = {} data = {}
...@@ -62,12 +55,6 @@ authDirective = ['$timeout', ($timeout) -> ...@@ -62,12 +55,6 @@ authDirective = ['$timeout', ($timeout) ->
scope.$apply -> scope.$apply ->
$target = angular.element event.target $target = angular.element event.target
$form = $target.controller('form') $form = $target.controller('form')
delete $form.responseErrorMessage
for ctrl in $form.$error.response?.slice?() or []
ctrl.$setValidity('response', true)
auth.submit($form) auth.submit($form)
scope.model = {} scope.model = {}
......
...@@ -4,11 +4,15 @@ createFormHelpers = -> ...@@ -4,11 +4,15 @@ createFormHelpers = ->
# API and updates the validity of the form. The field.$errors.response # API and updates the validity of the form. The field.$errors.response
# property will be true if there are errors and the responseErrorMessage # property will be true if there are errors and the responseErrorMessage
# will contain the API error message. # will contain the API error message.
applyValidationErrors: (form, errors) -> applyValidationErrors: (form, errors, reason) ->
for own field, error of errors for own field, error of errors
form[field].$setValidity('response', false) form[field].$setValidity('response', false)
form[field].responseErrorMessage = error form[field].responseErrorMessage = error
if reason
form.$setValidity('response', false)
form.responseErrorMessage = reason
angular.module('h.helpers.formHelpers', []) angular.module('h.helpers.formHelpers', [])
.factory('formHelpers', createFormHelpers) .factory('formHelpers', createFormHelpers)
...@@ -46,6 +46,7 @@ describe 'h.auth', -> ...@@ -46,6 +46,7 @@ describe 'h.auth', ->
auth.submit auth.submit
$name: 'login' $name: 'login'
$valid: true $valid: true
$setValidity: sandbox.stub()
assert.called session.$login assert.called session.$login
...@@ -53,13 +54,15 @@ describe 'h.auth', -> ...@@ -53,13 +54,15 @@ describe 'h.auth', ->
auth.submit auth.submit
$name: 'login' $name: 'login'
$valid: false $valid: false
$setValidity: sandbox.stub()
assert.notCalled session.$login assert.notCalled session.$login
it 'should set response errors', -> it 'should apply validation errors on submit', ->
form = form =
$name: 'register' $name: 'register'
$valid: true $valid: true
$setValidity: sandbox.stub()
username: username:
$setValidity: sandbox.stub() $setValidity: sandbox.stub()
email: email:
...@@ -68,8 +71,8 @@ describe 'h.auth', -> ...@@ -68,8 +71,8 @@ describe 'h.auth', ->
auth.submit(form) auth.submit(form)
assert.calledWith mockFormHelpers.applyValidationErrors, form, assert.calledWith mockFormHelpers.applyValidationErrors, form,
username: 'taken' {username: 'taken'},
assert.equal form.responseErrorMessage, 'registration error' 'registration error'
describe 'timeout', -> describe 'timeout', ->
it 'should happen after a period of inactivity', -> it 'should happen after a period of inactivity', ->
...@@ -120,17 +123,6 @@ describe 'h.auth', -> ...@@ -120,17 +123,6 @@ describe 'h.auth', ->
$scope = elem.isolateScope() $scope = elem.isolateScope()
it 'should reset response errors before submit', ->
$scope.login.username.$setViewValue('test')
$scope.login.password.$setViewValue('1234')
$scope.login.responseErrorMessage = 'test'
$scope.login.username.$setValidity('response', false)
assert.isFalse $scope.login.$valid
elem.find('input').trigger('submit')
assert.isTrue $scope.login.$valid
assert.isUndefined $scope.login.responseErrorMessage
it 'should invoke handlers set by attributes', -> it 'should invoke handlers set by attributes', ->
$rootScope.stub = sandbox.stub() $rootScope.stub = sandbox.stub()
for event in ['error', 'success', 'timeout'] for event in ['error', 'success', 'timeout']
......
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