codecombat/app/lib/forms.coffee
Ruben Vereecken e748417007 GET /auth/name/<name> now serves possible free names
anonymous users are now warned if their new name is already chosen

User Settings is now without auto-save

Upon name conflict, a user will be suggested a new name which is then submitted if the user chooses to save after all.

Refactored conflicted name checking so it can be used in more places

Signup form now has an optional name field

Covered extra case where the debounced check happened too late. Support for submitting on enter.

Worked in scott's comments and got tests working again
2014-07-14 20:07:58 +02:00

50 lines
1.6 KiB
CoffeeScript

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()
obj
module.exports.applyErrorsToForm = (el, errors, warning=false) ->
errors = [errors] if not $.isArray(errors)
for error in errors
if error.dataPath
prop = error.dataPath[1..]
console.log prop
message = error.message
else
message = "#{error.property} #{error.message}."
message = message[0].toUpperCase() + message[1..]
message = error.message if error.formatted
prop = error.property
setErrorToProperty el, prop, message, warning
module.exports.setErrorToField = setErrorToField = (el, message, warning=false) ->
formGroup = el.closest('.form-group')
unless formGroup.length
return console.error "#{el} did not contain a form group"
kind = if warning then 'warning' else 'error'
formGroup.addClass "has-#{kind}"
formGroup.append $("<span class='help-block #{kind}-help-block'>#{message}</span>")
module.exports.setErrorToProperty = setErrorToProperty = (el, property, message, warning=false) ->
input = $("[name='#{property}']", el)
unless input.length
return console.error "#{property} not found in #{el}"
setErrorToField input, message, warning
module.exports.clearFormAlerts = (el) ->
$('.has-error', el).removeClass('has-error')
$('.has-warning', el).removeClass('has-warning')
$('.alert.alert-danger', el).remove()
$('.alert.alert-warning', el).remove()
el.find('.help-block.error-help-block').remove()
el.find('.help-block.warning-help-block').remove()