Added support to show error message of object was not able to create.

This commit is contained in:
Martin Edenhofer 2016-02-16 16:41:33 +01:00
parent b338e10610
commit d9e9469f4d
8 changed files with 70 additions and 17 deletions

View file

@ -12,6 +12,10 @@ class App.ControllerForm extends App.Controller
# set empty class attributes if needed
if !@form
@form = @formGen()
# add alert placeholder
@form.prepend('<div class="alert alert--danger js-alert hide" role="alert"></div>')
if !@model
@model = {}
if !@attributes
@ -27,9 +31,18 @@ class App.ControllerForm extends App.Controller
@form.find('textarea').trigger('change')
@form.find('select').trigger('change')
# remove alert on input
@form.on('input', @hideAlert)
@finishForm = true
@form
showAlert: (message) =>
@form.find('.alert').removeClass('hide').html( App.i18n.translateContent( message ) )
hideAlert: =>
@form.find('.alert').addClass('hide').html()
html: =>
@form.html()

View file

@ -228,11 +228,11 @@ class Admin extends App.ControllerContent
)
@Config.set('system_init_done', true)
fail: (data) =>
fail: (settings, details) =>
@formEnable(e)
App.Event.trigger 'notify', {
type: 'error'
msg: App.i18n.translateContent( 'Can\'t create user!' )
msg: App.i18n.translateContent( details.error_human || 'Can\'t create user!' )
timeout: 2500
}
)
@ -984,11 +984,11 @@ class Agent extends App.ControllerContent
# rerender page
@render()
fail: (data) =>
fail: (settings, details) =>
@formEnable(e)
App.Event.trigger 'notify', {
type: 'error'
msg: App.i18n.translateContent( 'Can\'t create user!' )
msg: App.i18n.translateContent( details.error_human || 'Can\'t create user!' )
timeout: 2500
}
)

View file

@ -24,7 +24,7 @@ class Index extends App.ControllerContent
@html App.view('signup')()
new App.ControllerForm(
@form = new App.ControllerForm(
el: @el.find('form')
model: App.User
screen: 'signup'
@ -34,7 +34,7 @@ class Index extends App.ControllerContent
cancel: ->
@navigate '#login'
submit: (e) ->
submit: (e) =>
e.preventDefault()
@formDisable(e)
@params = @formParam(e.target)
@ -67,6 +67,9 @@ class Index extends App.ControllerContent
success: @success
error: @error
)
fail: (settings, details) =>
@formEnable(e)
@form.showAlert(details.error_human || details.error || 'Unable to update object!')
)
success: (data, status, xhr) =>

View file

@ -73,6 +73,7 @@ class _ajaxSingleton
# do not show any error message with code 401/404 (handled by controllers)
return if status is 401
return if status is 404
return if status is 422
# do not show any error message with code 502
return if status is 502

View file

@ -166,7 +166,14 @@ class Collection extends Base
failResponse: (options) =>
(xhr, statusText, error, settings) =>
@model.trigger('ajaxError', null, xhr, statusText, error, settings)
options.fail?.call(@model, settings)
# add errors to calllback
@record.trigger('ajaxError', @record, xhr, statusText, error, settings)
#options.fail?.call(@model, settings)
detailsRaw = xhr.responseText
if !_.isEmpty(detailsRaw)
details = JSON.parse(detailsRaw)
options.fail?.call(@record, settings, details)
# /add errors to calllback
class Singleton extends Base
constructor: (@record) ->
@ -230,8 +237,15 @@ class Singleton extends Base
switch settings.type
when 'POST' then @createFailed()
when 'DELETE' then @destroyFailed()
# add errors to calllback
@record.trigger('ajaxError', @record, xhr, statusText, error, settings)
options.fail?.call(@record, settings)
#options.fail?.call(@record, settings)
detailsRaw = xhr.responseText
if !_.isEmpty(detailsRaw)
details = JSON.parse(detailsRaw)
options.fail?.call(@record, settings, details)
@record.trigger('remove', @record)
# /add errors to calllback
createFailed: ->
@record.remove(clear: true)

View file

@ -307,7 +307,7 @@ class App.Model extends Spine.Model
# subscribe and render data / fetch new data if triggered
@bind(
'refresh change'
'refresh change remove'
(items) =>
App.Log.debug('Model', "local collection refresh/change #{@className}", items)
for key, callback of @SUBSCRIPTION_COLLECTION
@ -376,6 +376,19 @@ class App.Model extends Spine.Model
item = App[ @className ]._fillUp( item )
callback(item, 'change')
)
@bind(
'remove'
(items) =>
# check if result is array or singel item
if !_.isArray(items)
items = [items]
App.Log.debug('Model', "local remove #{@className}", items)
for item in items
for key, callback of App[ @className ].SUBSCRIPTION_ITEM[ item.id ]
item = App[ @className ]._fillUp( item )
callback(item, 'remove')
)
@changeTable = {}
@bind(

View file

@ -313,7 +313,7 @@ class ApplicationController < ActionController::Base
rescue => e
logger.error e.message
logger.error e.backtrace.inspect
render json: { error: e.message }, status: :unprocessable_entity
render json: model_match_error(e.message), status: :unprocessable_entity
end
def model_create_render_item (generic_object)
@ -335,7 +335,7 @@ class ApplicationController < ActionController::Base
rescue => e
logger.error e.message
logger.error e.backtrace.inspect
render json: { error: e.message }, status: :unprocessable_entity
render json: model_match_error(e.message), status: :unprocessable_entity
end
def model_update_render_item (generic_object)
@ -349,7 +349,7 @@ class ApplicationController < ActionController::Base
rescue => e
logger.error e.message
logger.error e.backtrace.inspect
render json: { error: e.message }, status: :unprocessable_entity
render json: model_match_error(e.message), status: :unprocessable_entity
end
def model_destory_render_item ()
@ -369,7 +369,7 @@ class ApplicationController < ActionController::Base
rescue => e
logger.error e.message
logger.error e.backtrace.inspect
render json: { error: e.message }, status: :unprocessable_entity
render json: model_match_error(e.message), status: :unprocessable_entity
end
def model_show_render_item (generic_object)
@ -382,11 +382,20 @@ class ApplicationController < ActionController::Base
rescue => e
logger.error e.message
logger.error e.backtrace.inspect
render json: { error: e.message }, status: :unprocessable_entity
render json: model_match_error(e.message), status: :unprocessable_entity
end
def model_index_render_result (generic_objects)
render json: generic_objects, status: :ok
end
def model_match_error (error)
data = {
error: error
}
if error =~ /(already exists|duplicate key)/i
data[:error_human] = 'Object already exists!'
end
data
end
end

View file

@ -77,7 +77,7 @@ class UsersController < ApplicationController
# check if feature is enabled
if !Setting.get('user_create_account')
render json: { error: 'Feature not enabled!' }, status: :unprocessable_entity
render json: { error_human: 'Feature not enabled!' }, status: :unprocessable_entity
return
end
@ -117,7 +117,7 @@ class UsersController < ApplicationController
if user.email
exists = User.where(email: user.email.downcase).first
if exists
render json: { error: 'User already exists!' }, status: :unprocessable_entity
render json: { error_human: 'User already exists!' }, status: :unprocessable_entity
return
end
end
@ -180,7 +180,7 @@ class UsersController < ApplicationController
user_new = User.find(user.id)
render json: user_new, status: :created
rescue => e
render json: { error: e.message }, status: :unprocessable_entity
render json: model_match_error(e.message), status: :unprocessable_entity
end
end