app/assets/javascripts/formagic/form.coffee in formagic-0.1.0 vs app/assets/javascripts/formagic/form.coffee in formagic-0.2.5
- old
+ new
@@ -120,11 +120,11 @@
inputClass ?= chr.formInputs['string']
inputName = if @config.namePrefix then "#{ @config.namePrefix }[#{ name }]" else "[#{ name }]"
# add prefix for nested form inputs
- if inputConfig.type == 'form' || inputConfig.type == 'documents'
+ if inputConfig.type == 'form' || inputConfig.type == 'documents' || inputConfig.type == 'document'
inputConfig.namePrefix = inputName.replace("[#{ name }]", "[#{ name }_attributes]")
else
inputConfig.namePrefix = @config.namePrefix
@@ -133,18 +133,24 @@
# NESTED ================================================
_add_nested_form_remove_button: ->
if @config.removeButton
+
# add hidden input to the form
- fieldName = '_destroy'
- input = @_render_input(fieldName, { type: 'hidden' }, false)
+ fieldName = '_destroy'
+ input = @_render_input(fieldName, {
+ type: 'hidden',
+ ignoreOnSubmission: @config.ignoreOnSubmission
+ }, false)
+
@inputs[fieldName] = input
- @$el.append input.$el
+ @$el.prepend input.$el
+
# remove button
@$removeButton =$ """<a href='#' class='nested-form-delete'>Delete</a>"""
- @$el.append @$removeButton
+ @$el.prepend @$removeButton
@$removeButton.on 'click', (e) =>
e.preventDefault()
if confirm('Are you sure?')
input.updateValue('true')
@$el.hide()
@@ -179,18 +185,24 @@
for form in @_forms()
# serialize file inputs for all forms (including nested)
for name, input of form.inputs
if input.config.type == 'file' or input.config.type == 'image'
file = input.$input.get()[0].files[0]
- obj["__FILE__#{ input.name }"] = file
+
+ if file
+ obj["__FILE__#{ input.name }"] = file
+
if input.isEmpty() then obj[input.removeName()] = 'true'
# remove fields with ignoreOnSubmission
- for name, input of form.inputs
+ for key, input of form.inputs
if input.config.ignoreOnSubmission
- delete obj[name]
+ delete obj[input.name]
+ # for k, v of obj
+ # console.log k
+
return obj
hash: (hash={}) ->
for name, input of @inputs
@@ -207,12 +219,20 @@
showValidationErrors: (errors) ->
@hideValidationErrors()
for inputName, messages of errors
- input = @inputs[inputName]
- firstMessage = messages[0]
- input.showErrorMessage(firstMessage)
+ input = @inputs[inputName]
+
+ # Support nested documents validation errors
+ if ! input
+ for k, v of @inputs
+ if v.name == "[#{ inputName }]"
+ input = v
+
+ if input
+ firstMessage = messages[0]
+ input.showErrorMessage(firstMessage)
hideValidationErrors: ->
for inputName, input of @inputs
input.hideErrorMessage()