bmichotte / ProMotion-XLForm

ProMotion-XLForm is a ProMotion plugin for XLForm
MIT License
20 stars 21 forks source link

Submitting a form the old fashion way... #28

Closed eliduke closed 9 years ago

eliduke commented 9 years ago

I am currently upgrading from Promotion-Form to XLForm and it's taking me a minute to wrap my brain around all the amazing, new, powerful options involved. Very excited about what is to come, but I can't seem to figure out how to simply just submit a form. It seems way more complicated than it needs to be.

It used to be that the submit button on the form would call the necessary action. Done and done. But now I have to have an on_click attribute in the cell that references a form_options :on_save callback thing that then calls the action that I want. I can certainly accept a bit of verbosity for better functionality, but now that adds a "Save" button in the nav_bar that I don't want.

Maybe this is all possible and I'm just not reading the docs right, but why can't I just do something like this...

def form_data
  [{
    title: "Support Request",
    cells: [{
      name: :body,
      title: nil,
      placeholder: "What can we help you with?",
      type: :textview,
    },{
      name: :send,
      title: "Send",
      type: :button,
      on_click: :send_message
    }]
  }]
end

def send_message(values)
  { do fancy api stuff with values }
end
bmichotte commented 9 years ago

You can do something like this

def form_data
  [{
    title: "Support Request",
    cells: [{
      name: :body,
      title: nil,
      placeholder: "What can we help you with?",
      type: :textview,
    },{
      name: :send,
      title: "Send",
      type: :button,
      on_click: :send_message
    }]
  }]
end

def send_message
   # check for error (validation, requirement, ...)
     unless valid?
        display_errors
        return
      end

    # self.values return a Hash with form_keys: value
    form_values = values
end
eliduke commented 9 years ago

I tried that, but I keep getting this error message:

xl_form_cell_builder.rb:99:in `block in create_cell:': undefined method `arity' for :send_message:Symbol

I dug around in the code base here:

# button clicks
if cell_data[:type] == :button && cell_data[:on_click]
  cell.action.formBlock = -> (cell) {
    action = cell_data[:on_click]
    case action.arity
      when 0
        action.call
      when 1
        action.call(cell)
      else
        mp(":on_click take 0 or 1 argument", force_color: :red)
    end
  }
end

I tried these alternatives:

on_click: send_message()
on_click: :'send_message:'
on_click: -> (cell) { :send_message }

None of them worked. Not sure how to handle that arity call.

bmichotte commented 9 years ago

you could try this

on_click: -> {
   send_message
}
bmichotte commented 9 years ago

(and I'll soon update the code to allow a Symbol as action as well)

eliduke commented 9 years ago

Sweet! That worked. Thanks. Also, I am trying to submit the form which sends a support request, then flash a message, and then just reset the form. I thought that I could use reload_form_data like from PM-Form, but that's not working. Did we lose that functionality in XLForm? Is there an equivalent?

bmichotte commented 9 years ago

There's nothing out-of the box to do it. what you can do is something like

some_field = cell_with_tag :some_field_name
some_field.value = nil
update_form_data

It seems I'll add a reset method as well ;)

eliduke commented 9 years ago

Oh, actually, update_form_data resets the form. It's all good!