codecombat/app/lib/forms.coffee

38 lines
1.1 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
module.exports.formToObject = (el) ->
obj = {}
inputs = $('input', el).add('textarea', el)
for input in inputs
input = $(input)
continue unless name = input.attr('name')
obj[name] = input.val()
2014-01-03 13:32:13 -05:00
obj
module.exports.applyErrorsToForm = (el, errors) ->
errors = [errors] if not $.isArray(errors)
missingErrors = []
for error in errors
if error.dataPath
prop = error.dataPath[1..]
message = error.message
else
message = "#{error.property} #{error.message}."
message = message[0].toUpperCase() + message[1..]
message = error.message if error.formatted
prop = error.property
input = $("[name='#{prop}']", el)
2014-01-27 14:30:22 -05:00
if not input.length
2014-01-03 13:32:13 -05:00
missingErrors.push(error)
continue
2014-01-27 14:30:22 -05:00
formGroup = input.closest('.form-group')
formGroup.addClass 'has-error'
2014-01-27 19:07:17 -05:00
formGroup.append($("<span class='help-block error-help-block'>#{message}</span>"))
2014-01-03 13:32:13 -05:00
return missingErrors
module.exports.clearFormAlerts = (el) ->
2014-01-27 14:30:22 -05:00
$('.has-error', el).removeClass('has-error')
$('.alert', el).remove()
2014-01-27 19:07:17 -05:00
el.find('.help-block.error-help-block').remove()