• Stars
    star
    609
  • Rank 73,614 (Top 2 %)
  • Language
    Ruby
  • License
    Other
  • Created over 12 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Making iOS Forms insanely great with RubyMotion

Formotion

Build Status Readme Score FOSSA Status

Make this:

Complex data form

using this:

@form = Formotion::Form.new({
  sections: [{
    title: "Register",
    rows: [{
      title: "Email",
      key: :email,
      placeholder: "[email protected]",
      type: :email,
      auto_correction: :no,
      auto_capitalization: :none
    }, {
      title: "Password",
      key: :password,
      placeholder: "required",
      type: :string,
      secure: true
    }, {
      title: "Password",
      subtitle: "Confirmation",
      key: :confirm,
      placeholder: "required",
      type: :string,
      secure: true
    }, {
      title: "Remember?",
      key: :remember,
      type: :switch,
    }]
  }, {
    title: "Account Type",
    key: :account_type,
    select_one: true,
    rows: [{
      title: "Free",
      key: :free,
      type: :check,
    }, {
      title: "Basic",
      value: true,
      key: :basic,
      type: :check,
    }, {
      title: "Pro",
      key: :pro,
      type: :check,
    }]
  }, {
    rows: [{
      title: "Sign Up",
      type: :submit,
    }]
  }]
})

@form_controller = Formotion::FormController.alloc.initWithForm(@form)
@window.rootViewController = @form_controller

And after the user enters some data, do this:

@form.render
=> {:email=>"[email protected]", :password=>"password", 
    :confirm=>"password", :remember=>true, :account_type=>:pro}

Installation

gem install formotion

In your Rakefile:

require 'formotion'

Usage

Initialize

You can initialize a Formotion::Form using either a hash (as above) or the DSL:

form = Formotion::Form.new

form.build_section do |section|
  section.title = "Title"

  section.build_row do |row|
    row.title = "Label"
    row.subtitle = "Placeholder"
    row.type = :string
  end
end

Then attach it to a Formotion::FormController and you're ready to rock and roll:

@controller = Formotion::FormController.alloc.initWithForm(form)
self.navigationController.pushViewController(@controller, animated: true)

Data Types

See the visual list of support row types.

To add your own, check the guide to adding new row types.

Formotion::Form, Formotion::Section, and Formotion::Row all respond to a ::PROPERTIES attribute. These are settable as an attribute (ie section.title = 'title') or in the initialization hash (ie {sections: [{title: 'title', ...}]}). Check the comments in the 3 main files (form.rb, section.rb, and row.rb for details on what these do).

Setting Initial Values

Forms, particularly edit forms, have default initial values. You can supply these in the :value attribute for a given row. So, for example:

{
  title: "Email",
  key: :email,
  placeholder: "[email protected]",
  type: :email,
  auto_correction: :no,
  auto_capitalization: :none,
  value: '[email protected]'
}

Setting values for non-string types can be tricky, so you need to watch what the particular field expects. In particular, date types require the number of seconds from the beginning of the epoch as a number.

Retrieve

You have form#submit, form#on_submit, and form#render at your disposal. Here's an example:

class PeopleController < Formotion::FormController
  def viewDidLoad
    super

    self.navigationItem.rightBarButtonItem = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemSave, target:self, action:'submit')
  end

  def submit
    data = self.form.render

    person.name = data[:name]
    person.address = data[:address]
  end
end

Why would you use form#on_submit? In case you want to use type: :submit. Ex:

@form = Formotion::Form.new({
  sections: [{
  ...
  }, {
    rows: [{
      title: "Save",
      type: :submit
    }]
  }]
})

@form.on_submit do |form|
  # do something with form.render
end

form#submit just triggers form#on_submit.

Persistence

You can easily synchronize a Form's state to disk using the persist_as key in form properties. When your user edits the form, any changes will be immediately saved. For example:

@form = Formotion::Form.new({
  persist_as: :settings,
  sections: ...
})

This will load the form if it exists, or create it if necessary. If you use remove the persist_as key, then the form will not be loaded and any changes you make will override existing saved forms.

Your form values and state are automatically persisted across application loads, no save buttons needed.

To reset the form, call reset, which restores it to the original state.

View the Persistence Example to see it in action.

Callbacks

Row objects support the following callbacks:

row.on_tap do |row|
  p "I'm tapped!"
end

row.on_delete do |row|
  p "I'm called before the delete animation"
end

row.on_begin do |row|
  p "I'm called when my text field begins editing"
end

row.on_enter do |row|
  p "I'm called when the user taps the return key while typing in my text field"
end

Forking

Feel free to fork and submit pull requests! And if you end up using Formotion in your app, I'd love to hear about your experience.

License

FOSSA Status

Todo

  • Not very efficient right now (creates a unique reuse idenitifer for each cell)
  • Styling/overriding the form for custom UITableViewDelegate/Data Source behaviors.

More Repositories

1

routable-ios

Routable, an in-app native URL router, for iOS
Objective-C
1,815
star
2

react.backbone

Plugin for React to make Backbone migration easier
JavaScript
840
star
3

routable-android

Routable, an in-app native URL router, for Android
Java
474
star
4

graphqlhub

The GraphQLHub Server
JavaScript
402
star
5

ShotBlocker

Detecting iOS screenshots ala Snapchat and Facebook Poke
Objective-C
257
star
6

rubymotion-tutorial

RubyMotion-Tutorial
Ruby
211
star
7

Walt

Fast, frictionless iOS animations
Ruby
144
star
8

HybridKit-iOS

HybridKit for iOS
Objective-C
126
star
9

remote_model

JSON API <-> NSObject via RubyMotion
Ruby
97
star
10

geomotion

Better iOS Geometry with RubyMotion
Ruby
90
star
11

Routable

A RubyMotion UIViewController -> URL router
Ruby
86
star
12

graphql-intro

JavaScript
86
star
13

react-vr

Write VR web apps with React JS
JavaScript
83
star
14

motion-toolbox.com

The Motion-Toolbox website
CSS
79
star
15

relay-101

Relay 101
JavaScript
48
star
16

HybridKit-JS

HybridKit for JavaScript
JavaScript
45
star
17

motion-screenshots

Automatic screenshots for your RubyMotion apps
Ruby
41
star
18

twittermotion

RubyMotion wrapper for the iOS Twitter API
Ruby
40
star
19

readme-score

Giving a score to README.md's everywhere
Ruby
40
star
20

motion-smarticons

Automatically generate icon labels for RubyMotion apps
Ruby
39
star
21

maglev

Faster, smoother iOS development
Ruby
36
star
22

motion-require

Dependency management for RubyMotion, using a pseudo `require`
Ruby
34
star
23

motion-env

Sync ENV variables between Rakefile and RubyMotion
Ruby
31
star
24

applebot

Automating iTunes Connect and the iOS Developer Center
JavaScript
31
star
25

react-native-remote-component

Load React Native components over the network
Objective-C
27
star
26

VolumeCamera

iPhone app where camera is triggered by volume buttons
Objective-C
26
star
27

urlp

A command-line URL parser, written in Rust
Rust
22
star
28

CLAFluxDispatcher

A port of Facebook's Flux Dispatcher to Objective-C
Objective-C
22
star
29

mixing-objc-ruby

Mixing Objective-C and Ruby with RubyMotion
Objective-C
20
star
30

typescript-1.8-tutorial

Migrating a JavaScript app to TypeScript 1.8
JavaScript
20
star
31

motion-colorize

Add some color to your RubyMotion output
Ruby
17
star
32

motion-screenspecs

Test your RubyMotion app using screenshot comparison
Ruby
17
star
33

graphql-unions-interfaces

JavaScript
13
star
34

appleload

CLI & Ruby Library to control Apple's Application Loader app
Ruby
12
star
35

motion-weakattr

weak_attr for WeakRefs in RubyMotion
Ruby
10
star
36

appleslice

Easily parse Apple & iTunes Connect emails
Ruby
7
star
37

motion-http

Cross-platform HTTP library for RubyMotion
7
star
38

BrocabularyTutorial

The source to the Android App From Scratch tutorials
Java
7
star
39

relay-102

Relay with Mutations! Powered by GraphQLHub
JavaScript
6
star
40

plottable-examples

HTML
5
star
41

afmotion-rocket

AFMotion extensions for Rocket
Ruby
5
star
42

CYStoreExample

Magically easy in-app purchases on iOS.
Objective-C
4
star
43

MotionCorePlot

Ruby
3
star
44

graphql-directives

JavaScript
3
star
45

motion-tips

Motion Tips blog dump.
Ruby
3
star
46

gumroad-ruby

Ruby binding for Gumroad API
Ruby
3
star
47

smashing-rubymotion

Source code to Smashing's RubyMotion example
Ruby
3
star
48

droidproj

Manage Android projects with a Droidfile
Ruby
2
star
49

readme-score-api

The API for readme-score
Go
2
star
50

FormotionTest

Tests
Ruby
2
star
51

WebActivity

Drop-in Activity to browse the web
Java
1
star
52

new-news

TypeScript
1
star
53

botlib

The shared library for AppleBot and PlayBot
Ruby
1
star
54

clayallsopp.github.com

1
star