Rubymotion: Automatically submitting a formotion form when form changed

177 Views Asked by At

I would like to submit the formotion form to a server when I changed a field in the form. Somebody knows if thereis a change callback or something like that?

My form:

class SettingsScreen < PM::FormotionScreen
  title "Settings"
  tab_bar_item title: "Settings", icon: 'settings'

  def table_data
    {
      sections: [
        {
          title: "Settings",
          rows: [
            {
              title: "Start Date",
              key: :start_date,
              type: :date,
              format: :medium
            }, {
              title: "End Date",
              key: :end_date,
              type: :date,
              format: :medium
            }
          ]
        }
      ]
    }
  end

  def on_load
    self.form.on_submit do |form|
      p "submitting!"
    end
  end
end
2

There are 2 best solutions below

0
Jamon Holmgren On

One way would be to use RMQ to watch the values.

An example (untested) of what you could do with it:

rmq(UITextView).on(:change) do |sender|
  save_something @form.render
end

There's an undocumented (as far as I know) on_change method on rows, but I'm not totally sure how to get the rows in the first place.

https://github.com/clayallsopp/formotion/search?q=on_change&ref=cmdform

0
Damien Justin Šutevski On

Here's an example from the current app I'm working on. I'm using Bubblewrap's observer, but I imagine RMQ would work the same. You can just put whatever call you like in that observe block. Also, I do recall the undocumented methods Jamon refers to to work - they just weren't working for my specific need. Getting the rows outside of the actual form is more of a hassle but also do-able.

section.build_row do |row|
    row.title = "Percentage"

    row.type = :static
    row.value = @percentage.to_i.to_s + ' %' || ''

  end
  section.build_row do |row|
    row.title = ""
    row.key = :percentage
    row.type = :slider
    row.range = (1..100)
    row.value = @percentage || 50
    observe(row, "value") do |old_value, new_value|
        row.previous_row.value = new_value.to_i.to_s + ' %'
    end
  end