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
failure = (form, response) ->
{errors, reason} = response.data
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)
formHelpers.applyValidationErrors(form, errors, reason)
this.submit = (form) ->
delete form.responseErrorMessage
form.$setValidity('response', true)
return unless form.$valid
data = {}
......@@ -62,12 +55,6 @@ authDirective = ['$timeout', ($timeout) ->
scope.$apply ->
$target = angular.element event.target
$form = $target.controller('form')
delete $form.responseErrorMessage
for ctrl in $form.$error.response?.slice?() or []
ctrl.$setValidity('response', true)
auth.submit($form)
scope.model = {}
......
......@@ -4,11 +4,15 @@ createFormHelpers = ->
# API and updates the validity of the form. The field.$errors.response
# property will be true if there are errors and the responseErrorMessage
# will contain the API error message.
applyValidationErrors: (form, errors) ->
applyValidationErrors: (form, errors, reason) ->
for own field, error of errors
form[field].$setValidity('response', false)
form[field].responseErrorMessage = error
if reason
form.$setValidity('response', false)
form.responseErrorMessage = reason
angular.module('h.helpers.formHelpers', [])
.factory('formHelpers', createFormHelpers)
......@@ -46,6 +46,7 @@ describe 'h.auth', ->
auth.submit
$name: 'login'
$valid: true
$setValidity: sandbox.stub()
assert.called session.$login
......@@ -53,13 +54,15 @@ describe 'h.auth', ->
auth.submit
$name: 'login'
$valid: false
$setValidity: sandbox.stub()
assert.notCalled session.$login
it 'should set response errors', ->
it 'should apply validation errors on submit', ->
form =
$name: 'register'
$valid: true
$setValidity: sandbox.stub()
username:
$setValidity: sandbox.stub()
email:
......@@ -68,8 +71,8 @@ describe 'h.auth', ->
auth.submit(form)
assert.calledWith mockFormHelpers.applyValidationErrors, form,
username: 'taken'
assert.equal form.responseErrorMessage, 'registration error'
{username: 'taken'},
'registration error'
describe 'timeout', ->
it 'should happen after a period of inactivity', ->
......@@ -120,17 +123,6 @@ describe 'h.auth', ->
$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', ->
$rootScope.stub = sandbox.stub()
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