formtastic 3.1.5 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +5 -5
  2. data/.gitattributes +1 -0
  3. data/.github/workflows/test.yml +61 -0
  4. data/.gitignore +3 -2
  5. data/CHANGELOG.md +52 -0
  6. data/Gemfile.lock +105 -0
  7. data/MIT-LICENSE +1 -1
  8. data/README.md +627 -629
  9. data/RELEASE_PROCESS +3 -1
  10. data/Rakefile +20 -1
  11. data/app/assets/stylesheets/formtastic.css +1 -1
  12. data/bin/appraisal +8 -0
  13. data/formtastic.gemspec +9 -15
  14. data/gemfiles/rails_5.2/Gemfile +5 -0
  15. data/gemfiles/rails_6.0/Gemfile +5 -0
  16. data/gemfiles/rails_6.1/Gemfile +5 -0
  17. data/gemfiles/rails_edge/Gemfile +13 -0
  18. data/lib/formtastic.rb +5 -11
  19. data/lib/formtastic/actions.rb +6 -3
  20. data/lib/formtastic/deprecation.rb +1 -38
  21. data/lib/formtastic/engine.rb +3 -1
  22. data/lib/formtastic/form_builder.rb +8 -24
  23. data/lib/formtastic/helpers/action_helper.rb +1 -48
  24. data/lib/formtastic/helpers/errors_helper.rb +2 -2
  25. data/lib/formtastic/helpers/fieldset_wrapper.rb +7 -3
  26. data/lib/formtastic/helpers/input_helper.rb +18 -76
  27. data/lib/formtastic/helpers/inputs_helper.rb +12 -3
  28. data/lib/formtastic/i18n.rb +1 -1
  29. data/lib/formtastic/inputs.rb +32 -29
  30. data/lib/formtastic/inputs/base/collections.rb +1 -5
  31. data/lib/formtastic/inputs/base/errors.rb +4 -4
  32. data/lib/formtastic/inputs/base/hints.rb +1 -1
  33. data/lib/formtastic/inputs/base/timeish.rb +5 -1
  34. data/lib/formtastic/inputs/base/validations.rb +19 -9
  35. data/lib/formtastic/inputs/check_boxes_input.rb +3 -3
  36. data/lib/formtastic/inputs/color_input.rb +0 -1
  37. data/lib/formtastic/inputs/select_input.rb +1 -1
  38. data/lib/formtastic/localizer.rb +5 -7
  39. data/lib/formtastic/version.rb +1 -1
  40. data/lib/generators/templates/formtastic.rb +4 -6
  41. data/sample/basic_inputs.html +1 -1
  42. data/script/integration-template.rb +74 -0
  43. data/script/integration.sh +19 -0
  44. data/spec/action_class_finder_spec.rb +1 -1
  45. data/spec/actions/button_action_spec.rb +8 -8
  46. data/spec/actions/generic_action_spec.rb +60 -60
  47. data/spec/actions/input_action_spec.rb +7 -7
  48. data/spec/actions/link_action_spec.rb +10 -10
  49. data/spec/builder/custom_builder_spec.rb +37 -21
  50. data/spec/builder/error_proc_spec.rb +4 -4
  51. data/spec/builder/semantic_fields_for_spec.rb +27 -27
  52. data/spec/generators/formtastic/form/form_generator_spec.rb +25 -25
  53. data/spec/generators/formtastic/input/input_generator_spec.rb +31 -31
  54. data/spec/generators/formtastic/install/install_generator_spec.rb +9 -9
  55. data/spec/helpers/action_helper_spec.rb +328 -10
  56. data/spec/helpers/actions_helper_spec.rb +17 -17
  57. data/spec/helpers/form_helper_spec.rb +33 -33
  58. data/spec/helpers/input_helper_spec.rb +975 -2
  59. data/spec/helpers/inputs_helper_spec.rb +120 -105
  60. data/spec/helpers/reflection_helper_spec.rb +3 -3
  61. data/spec/helpers/semantic_errors_helper_spec.rb +22 -22
  62. data/spec/i18n_spec.rb +26 -26
  63. data/spec/input_class_finder_spec.rb +1 -1
  64. data/spec/inputs/base/collections_spec.rb +6 -6
  65. data/spec/inputs/base/validations_spec.rb +157 -19
  66. data/spec/inputs/boolean_input_spec.rb +55 -55
  67. data/spec/inputs/check_boxes_input_spec.rb +96 -95
  68. data/spec/inputs/color_input_spec.rb +51 -63
  69. data/spec/inputs/country_input_spec.rb +20 -20
  70. data/spec/inputs/custom_input_spec.rb +2 -6
  71. data/spec/inputs/datalist_input_spec.rb +1 -1
  72. data/spec/inputs/date_picker_input_spec.rb +42 -42
  73. data/spec/inputs/date_select_input_spec.rb +51 -37
  74. data/spec/inputs/datetime_picker_input_spec.rb +46 -46
  75. data/spec/inputs/datetime_select_input_spec.rb +53 -37
  76. data/spec/inputs/email_input_spec.rb +5 -5
  77. data/spec/inputs/file_input_spec.rb +6 -6
  78. data/spec/inputs/hidden_input_spec.rb +18 -18
  79. data/spec/inputs/include_blank_spec.rb +8 -8
  80. data/spec/inputs/label_spec.rb +20 -20
  81. data/spec/inputs/number_input_spec.rb +112 -112
  82. data/spec/inputs/password_input_spec.rb +5 -5
  83. data/spec/inputs/phone_input_spec.rb +5 -5
  84. data/spec/inputs/placeholder_spec.rb +5 -5
  85. data/spec/inputs/radio_input_spec.rb +63 -65
  86. data/spec/inputs/range_input_spec.rb +66 -66
  87. data/spec/inputs/readonly_spec.rb +4 -4
  88. data/spec/inputs/search_input_spec.rb +5 -5
  89. data/spec/inputs/select_input_spec.rb +92 -96
  90. data/spec/inputs/string_input_spec.rb +23 -23
  91. data/spec/inputs/text_input_spec.rb +16 -16
  92. data/spec/inputs/time_picker_input_spec.rb +43 -43
  93. data/spec/inputs/time_select_input_spec.rb +67 -54
  94. data/spec/inputs/time_zone_input_spec.rb +19 -19
  95. data/spec/inputs/url_input_spec.rb +5 -5
  96. data/spec/inputs/with_options_spec.rb +7 -7
  97. data/spec/localizer_spec.rb +17 -17
  98. data/spec/namespaced_class_finder_spec.rb +2 -2
  99. data/spec/schema.rb +21 -0
  100. data/spec/spec_helper.rb +163 -223
  101. data/spec/support/custom_macros.rb +72 -75
  102. data/spec/support/shared_examples.rb +0 -1301
  103. data/spec/support/test_environment.rb +23 -9
  104. metadata +36 -126
  105. data/.travis.yml +0 -46
  106. data/Appraisals +0 -43
  107. data/CHANGELOG +0 -54
  108. data/DEPRECATIONS +0 -52
  109. data/gemfiles/rails_3.2.gemfile +0 -9
  110. data/gemfiles/rails_4.0.4.gemfile +0 -8
  111. data/gemfiles/rails_4.1.gemfile +0 -8
  112. data/gemfiles/rails_4.2.gemfile +0 -8
  113. data/gemfiles/rails_4.gemfile +0 -8
  114. data/gemfiles/rails_5.0.gemfile +0 -8
  115. data/gemfiles/rails_edge.gemfile +0 -15
  116. data/lib/formtastic/util.rb +0 -57
  117. data/spec/helpers/namespaced_action_helper_spec.rb +0 -43
  118. data/spec/helpers/namespaced_input_helper_spec.rb +0 -36
  119. data/spec/util_spec.rb +0 -66
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 7fda601161112f0ec859c833e04fce3155e56af0
4
- data.tar.gz: dc138bd2308a2ab393c9badfab273800b985341a
2
+ SHA256:
3
+ metadata.gz: cb44ea01df970135604cdddd0ac7b762ea56077f915810c4c8fe6be3487cafa3
4
+ data.tar.gz: 18630a8282b76e40d0c6e9faad19f99112dfd9e22ba22e7b025d8730ebf26ace
5
5
  SHA512:
6
- metadata.gz: 838af637ca0cb0de6f086e76dcf8306c7436901eb3811afe4e15c38c57de7f16eec795ac71090fd648eceba93c488bb80c1958124225398071905d0498518c9f
7
- data.tar.gz: 326c8f5b7da290dedb55be38e9cf428ba0051e98c097addd0e5e0270837aa479ae6ee68350965f681c9de83228aa40702cbb74c4bff6d3f1b0488c1920e0537b
6
+ metadata.gz: 73a3ad17e91765150e4e15e03d3031f5b9efd975a09bbb21c7f404709b7d2462e9cf16c708beaf2745b783def3a631ca1b3bd47278d79c16012d79ba1f549fc0
7
+ data.tar.gz: d418a7e65f928dedfd36627c3ece7c4fe7e371984500ddc4bcb6c5929087264541476c469be6520270000a0e8348bc2cd41415e94d46c86da2e48021bf54d4be
data/.gitattributes ADDED
@@ -0,0 +1 @@
1
+ CHANGELOG merge=union
@@ -0,0 +1,61 @@
1
+ name: test
2
+
3
+ on:
4
+ push:
5
+ pull_request:
6
+
7
+ jobs:
8
+ test:
9
+ runs-on: ubuntu-20.04
10
+
11
+ strategy:
12
+ fail-fast: false
13
+
14
+ matrix:
15
+ gemfile:
16
+ - gemfiles/rails_5.2/Gemfile
17
+ - gemfiles/rails_6.0/Gemfile
18
+ - gemfiles/rails_6.1/Gemfile
19
+ - gemfiles/rails_edge/Gemfile
20
+
21
+ ruby:
22
+ - 2.4.10
23
+ - 2.5.8
24
+ - 2.6.6
25
+ - 2.7.2
26
+ - 3.0.0
27
+
28
+ exclude:
29
+ - ruby: 2.4.10
30
+ gemfile: gemfiles/rails_6.0/Gemfile
31
+
32
+ - ruby: 2.4.10
33
+ gemfile: gemfiles/rails_6.1/Gemfile
34
+
35
+ - ruby: 2.4.10
36
+ gemfile: gemfiles/rails_edge/Gemfile
37
+
38
+ - ruby: 3.0.0
39
+ gemfile: gemfiles/rails_5.2/Gemfile
40
+
41
+ env:
42
+ BUNDLE_GEMFILE: ${{ matrix.gemfile }}
43
+
44
+ steps:
45
+ - uses: actions/checkout@v2
46
+
47
+ - uses: actions/setup-node@v2
48
+ with:
49
+ node-version: 11
50
+
51
+ - name: Set up Ruby
52
+ uses: ruby/setup-ruby@v1
53
+ with:
54
+ ruby-version: ${{ matrix.ruby }}
55
+ bundler-cache: true
56
+
57
+ - name: Run tests
58
+ run: |
59
+ bundle exec rake spec
60
+ script/integration.sh
61
+ continue-on-error: ${{ matrix.gemfile == 'gemfiles/rails_edge/Gemfile' }}
data/.gitignore CHANGED
@@ -9,9 +9,10 @@ pkg
9
9
  log/*
10
10
  .rvmrc
11
11
  .ruby-version
12
+ \.ruby-gemset
12
13
  .bundle
13
- Gemfile.lock
14
14
  .yardoc
15
15
  doc/
16
16
  tmp
17
- gemfiles/*.lock
17
+ gemfiles/*/Gemfile.lock
18
+ /dummy/
data/CHANGELOG.md ADDED
@@ -0,0 +1,52 @@
1
+ ## master / 4.0.0
2
+ * Fixed default_columns_for_object when object has non-standard foreign keys (#1241)
3
+ * Fixed missing constants in production (#911)
4
+ * Removed support for Rails 3 and 4.0 (#1108)
5
+ * Removed deprecated input/action finder methods (#1139)
6
+ * Changed boolean label padding to margin (#1202)
7
+ * Added mapping hstore column to text input (#1203)
8
+ * Added support for Rails 5 Attributes API (#1188)
9
+ * Changed required Ruby version to >= 2.0 (#1210)
10
+ * Default to input types text for json & jsonb, string for citext columns (#1229)
11
+ * Allow symbols for numericality options (#1258)
12
+ * Support for rubies under 2.4.0 has been dropped (#1292)
13
+ * Support for Rails under 5.2.0 has been dropped (#1293)
14
+ * Support for Rails 6.0 has been added (#1300)
15
+ * Support for Rails 6.1 has been added (#1324)
16
+ * Support for Ruby 3 has been added (#1323)
17
+
18
+ ## 3.1.2
19
+
20
+ * Fixed that we specified 4.0.4 instead of 4.1 in the Rails version deprecation message
21
+
22
+ ## 3.1.1
23
+
24
+ * Fixed class custom input & action class loading in test environments
25
+ * Added documentation of custom input & action class finders
26
+ * Added a link to documentation & wiki from custom class deprecation warnings
27
+
28
+ ## 3.1.0
29
+
30
+ * Performance and documentation improvements
31
+
32
+ ## 3.1.0.rc2
33
+
34
+ * Deprecated :member_value and :member_label options
35
+
36
+ ## 3.1.0.rc1
37
+
38
+ * Deprecated support for Rails version < 4.1.0
39
+ * Fixed synchronization issues with custom_namespace configuration
40
+ * Fixed bug where boolean (checkbox) inputs were not being correctly checked (also in 2.3.1)
41
+ * Fixed (silenced) Rails 5 deprecation on column_for_attribute that we're handling fine
42
+ * Added new DatalistInput (:as => :datalist) for HTML5 datalists
43
+ * Added configurable namespaces for custom inputs
44
+ * Various performance and documentation improvements
45
+
46
+ ---
47
+
48
+ See 3.0-stable branch for 3.0.x changes
49
+ https://github.com/formtastic/formtastic/blob/3.0-stable/CHANGELOG
50
+
51
+ See 2.3-stable branch for 2.3.x and earlier releases
52
+ https://github.com/formtastic/formtastic/blob/2.3-stable/CHANGELOG
data/Gemfile.lock ADDED
@@ -0,0 +1,105 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ formtastic (4.0.0.rc1)
5
+ actionpack (>= 5.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (6.1.1)
11
+ actionview (= 6.1.1)
12
+ activesupport (= 6.1.1)
13
+ rack (~> 2.0, >= 2.0.9)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (6.1.1)
18
+ activesupport (= 6.1.1)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activesupport (6.1.1)
24
+ concurrent-ruby (~> 1.0, >= 1.0.2)
25
+ i18n (>= 1.6, < 2)
26
+ minitest (>= 5.1)
27
+ tzinfo (~> 2.0)
28
+ zeitwerk (~> 2.3)
29
+ ammeter (1.1.4)
30
+ activesupport (>= 3.0)
31
+ railties (>= 3.0)
32
+ rspec-rails (>= 2.2)
33
+ builder (3.2.4)
34
+ concurrent-ruby (1.1.8)
35
+ crass (1.0.6)
36
+ diff-lcs (1.3)
37
+ erubi (1.10.0)
38
+ i18n (1.8.7)
39
+ concurrent-ruby (~> 1.0)
40
+ loofah (2.9.0)
41
+ crass (~> 1.0.2)
42
+ nokogiri (>= 1.5.9)
43
+ method_source (1.0.0)
44
+ mini_portile2 (2.5.0)
45
+ minitest (5.14.3)
46
+ nokogiri (1.11.1)
47
+ mini_portile2 (~> 2.5.0)
48
+ racc (~> 1.4)
49
+ racc (1.5.2)
50
+ rack (2.2.3)
51
+ rack-test (1.1.0)
52
+ rack (>= 1.0, < 3)
53
+ rails-dom-testing (2.0.3)
54
+ activesupport (>= 4.2.0)
55
+ nokogiri (>= 1.6)
56
+ rails-html-sanitizer (1.3.0)
57
+ loofah (~> 2.3)
58
+ railties (6.1.1)
59
+ actionpack (= 6.1.1)
60
+ activesupport (= 6.1.1)
61
+ method_source
62
+ rake (>= 0.8.7)
63
+ thor (~> 1.0)
64
+ rake (13.0.3)
65
+ rspec-core (3.9.2)
66
+ rspec-support (~> 3.9.3)
67
+ rspec-dom-testing (0.1.0)
68
+ rails-dom-testing (>= 1.0, < 3)
69
+ rspec-expectations (~> 3.0)
70
+ rspec-expectations (3.9.2)
71
+ diff-lcs (>= 1.2.0, < 2.0)
72
+ rspec-support (~> 3.9.0)
73
+ rspec-mocks (3.9.1)
74
+ diff-lcs (>= 1.2.0, < 2.0)
75
+ rspec-support (~> 3.9.0)
76
+ rspec-rails (3.9.1)
77
+ actionpack (>= 3.0)
78
+ activesupport (>= 3.0)
79
+ railties (>= 3.0)
80
+ rspec-core (~> 3.9.0)
81
+ rspec-expectations (~> 3.9.0)
82
+ rspec-mocks (~> 3.9.0)
83
+ rspec-support (~> 3.9.0)
84
+ rspec-support (3.9.3)
85
+ sqlite3 (1.4.2)
86
+ thor (1.1.0)
87
+ tzinfo (2.0.4)
88
+ concurrent-ruby (~> 1.0)
89
+ yard (0.9.25)
90
+ zeitwerk (2.4.2)
91
+
92
+ PLATFORMS
93
+ ruby
94
+
95
+ DEPENDENCIES
96
+ ammeter (~> 1.1.3)
97
+ formtastic!
98
+ rake
99
+ rspec-dom-testing (>= 0.1.0)
100
+ rspec-rails (~> 3.4)
101
+ sqlite3 (~> 1.4)
102
+ yard (~> 0.9.20)
103
+
104
+ BUNDLED WITH
105
+ 2.1.4
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008-2010 Justin French
1
+ Copyright (c) 2008-2021
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,629 +1,627 @@
1
- # Formtastic
2
-
3
- [![Build Status](https://travis-ci.org/justinfrench/formtastic.svg?branch=master)](https://travis-ci.org/justinfrench/formtastic)
4
- [![Inline docs](http://inch-ci.org/github/justinfrench/formtastic.svg?branch=master)](http://inch-ci.org/github/justinfrench/formtastic)
5
- [![Code Climate](https://codeclimate.com/github/justinfrench/formtastic/badges/gpa.svg)](https://codeclimate.com/github/justinfrench/formtastic)
6
- [![Gem Version](https://badge.fury.io/rb/formtastic.svg)](https://badge.fury.io/rb/formtastic)
7
- [![Dependency](https://gemnasium.com/justinfrench/formtastic.png)](https://gemnasium.com/justinfrench/formtastic)
8
-
9
- Formtastic is a Rails FormBuilder DSL (with some other goodies) to make it far easier to create beautiful, semantically rich, syntactically awesome, readily stylable and wonderfully accessible HTML forms in your Rails applications.
10
-
11
- ## Documentation & Support
12
-
13
- * [Documentation is available on rdoc.info](http://rdoc.info/projects/justinfrench/formtastic)
14
- * [We track issues & bugs on GitHub](http://github.com/justinfrench/formtastic/issues)
15
- * [We have a wiki on GitHub](http://github.com/justinfrench/formtastic/wiki)
16
- * [StackOverflow can help](http://stackoverflow.com/questions/tagged/formtastic)
17
- * [Follow @formtastic on Twitter for news & updates](http://twitter.com/formtastic)
18
-
19
- ## Compatibility
20
-
21
- * Formtastic 4 will require Rails 4.1
22
- * Formtastic 3 requires Rails 3.2.13 minimum
23
- * Formtastic 2 requires Rails 3
24
- * Formtastic, much like Rails, is very ActiveRecord-centric. Many are successfully using other ActiveModel-like ORMs and objects (DataMapper, MongoMapper, Mongoid, Authlogic, Devise...) but we're not guaranteeing full compatibility at this stage. Patches are welcome!
25
-
26
- ## The Story
27
-
28
- One day, I finally had enough, so I opened up my text editor, and wrote a DSL for how I'd like to author forms:
29
-
30
- ```erb
31
- <%= semantic_form_for @article do |f| %>
32
-
33
- <%= f.inputs :name => "Basic" do %>
34
- <%= f.input :title %>
35
- <%= f.input :body %>
36
- <%= f.input :section %>
37
- <%= f.input :publication_state, :as => :radio %>
38
- <%= f.input :category %>
39
- <%= f.input :allow_comments, :label => "Allow commenting on this article" %>
40
- <% end %>
41
-
42
- <%= f.inputs :name => "Advanced" do %>
43
- <%= f.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
44
- <%= f.input :extract, :required => false %>
45
- <%= f.input :description, :required => false %>
46
- <%= f.input :url_title, :required => false %>
47
- <% end %>
48
-
49
- <%= f.inputs :name => "Author", :for => :author do |author_form| %>
50
- <%= author_form.input :first_name %>
51
- <%= author_form.input :last_name %>
52
- <% end %>
53
-
54
- <%= f.actions do %>
55
- <%= f.action :submit, :as => :button %>
56
- <%= f.action :cancel, :as => :link %>
57
- <% end %>
58
-
59
- <% end %>
60
- ```
61
-
62
- I also wrote the accompanying HTML output I expected, favoring something very similar to the fieldsets, lists and other semantic elements Aaron Gustafson presented in [Learning to Love Forms](http://www.slideshare.net/AaronGustafson/learning-to-love-forms-web-directions-south-07), hacking together enough Ruby to prove it could be done.
63
-
64
-
65
- ## It's awesome because...
66
-
67
- * It can handle `belongs_to` associations (like Post belongs_to :author), rendering a select or set of radio inputs with choices from the parent model.
68
- * It can handle `has_many` and `has_and_belongs_to_many` associations (like: Post has_many :tags), rendering a multi-select with choices from the child models.
69
- * It's Rails 3/4 compatible (including nested forms).
70
- * It has internationalization (I18n)!
71
- * It's _really_ quick to get started with a basic form in place (4 lines), then go back to add in more detail if you need it.
72
- * There's heaps of elements, id and class attributes for you to hook in your CSS and JS.
73
- * It handles real world stuff like inline hints, inline error messages & help text.
74
- * It doesn't hijack or change any of the standard Rails form inputs, so you can still use them as expected (even mix and match).
75
- * It's got absolutely awesome spec coverage.
76
- * There's a bunch of people using and working on it (it's not just one developer building half a solution).
77
- * It has growing HTML5 support (new inputs like email/phone/search, new attributes like required/min/max/step/placeholder)
78
-
79
-
80
- ## Opinions
81
-
82
- * It should be easier to do things the right way than the wrong way.
83
- * Sometimes _more mark-up_ is better.
84
- * Elements and attribute hooks are _gold_ for stylesheet authors.
85
- * Make the common things we do easy, yet ensure uncommon things are still possible.
86
-
87
-
88
- ## Installation
89
-
90
- Simply add Formtastic to your Gemfile and bundle it up:
91
-
92
- ```ruby
93
- gem 'formtastic', '~> 3.0'
94
- ```
95
-
96
- Run the installation generator:
97
-
98
- ```shell
99
- $ rails generate formtastic:install
100
- ```
101
-
102
-
103
- ## Stylesheets
104
-
105
- A proof-of-concept set of stylesheets are provided which you can include in your layout. Customization is best achieved by overriding these styles in an additional stylesheet.
106
-
107
- Rails 3.1 introduces an asset pipeline that allows plugins like Formtastic to serve their own Stylesheets, Javascripts, etc without having to run generators that copy them across to the host application. Formtastic makes three stylesheets available as an Engine, you just need to require them in your global stylesheets.
108
-
109
- ```css
110
- # app/assets/stylesheets/application.css
111
- *= require formtastic
112
- *= require my_formtastic_changes
113
- ```
114
-
115
- Conditional stylesheets need to be compiled separately to prevent them being bundled and included with other application styles. Remove `require_tree .` from application.css and specify required stylesheets individually.
116
-
117
- ```css
118
- # app/assets/stylesheets/ie6.css
119
- *= require formtastic_ie6
120
-
121
- # app/assets/stylesheets/ie7.css
122
- *= require formtastic_ie7
123
- ```
124
-
125
- ```erb
126
- # app/views/layouts/application.html.erb
127
- <%= stylesheet_link_tag 'application' %>
128
- <!--[if IE 6]><%= stylesheet_link_tag 'ie6' %><![endif]-->
129
- <!--[if IE 7]><%= stylesheet_link_tag 'ie7' %><![endif]-->
130
- ```
131
-
132
- ```ruby
133
- # config/environments/production.rb
134
- config.assets.precompile += %w( ie6.css ie7.css )
135
- ```
136
-
137
- ## Usage
138
-
139
- Forms are really boring to code... you want to get onto the good stuff as fast as possible.
140
-
141
- This renders a set of inputs (one for _most_ columns in the database table, and one for each ActiveRecord `belongs_to`-association), followed by default action buttons (an input submit button):
142
-
143
- ```erb
144
- <%= semantic_form_for @user do |f| %>
145
- <%= f.inputs %>
146
- <%= f.actions %>
147
- <% end %>
148
- ```
149
-
150
- This is a great way to get something up fast, but like scaffolding, it's *not recommended for production*. Don't be so lazy!
151
-
152
- To specify the order of the fields, skip some of the fields or even add in fields that Formtastic couldn't infer. You can pass in a list of field names to `inputs` and list of action names to `actions`:
153
-
154
- ```erb
155
- <%= semantic_form_for @user do |f| %>
156
- <%= f.inputs :title, :body, :section, :categories, :created_at %>
157
- <%= f.actions :submit, :cancel %>
158
- <% end %>
159
- ```
160
-
161
- You probably want control over the input type Formtastic uses for each field. You can expand the `inputs` and `actions` to block helper format and use the `:as` option to specify an exact input type:
162
-
163
- ```erb
164
- <%= semantic_form_for @post do |f| %>
165
- <%= f.inputs do %>
166
- <%= f.input :title %>
167
- <%= f.input :body %>
168
- <%= f.input :section, :as => :radio %>
169
- <%= f.input :categories %>
170
- <%= f.input :created_at, :as => :string %>
171
- <% end %>
172
- <%= f.actions do %>
173
- <%= f.action :submit, :as => :button %>
174
- <%= f.action :cancel, :as => :link %>
175
- <% end %>
176
- <% end %>
177
- ```
178
-
179
- If you want to customize the label text, or render some hint text below the field, specify which fields are required/optional, or break the form into two fieldsets, the DSL is pretty comprehensive:
180
-
181
- ```erb
182
- <%= semantic_form_for @post do |f| %>
183
- <%= f.inputs "Basic", :id => "basic" do %>
184
- <%= f.input :title %>
185
- <%= f.input :body %>
186
- <% end %>
187
- <%= f.inputs :name => "Advanced Options", :id => "advanced" do %>
188
- <%= f.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
189
- <%= f.input :section, :as => :radio %>
190
- <%= f.input :user, :label => "Author" %>
191
- <%= f.input :categories, :required => false %>
192
- <%= f.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
193
- <% end %>
194
- <%= f.actions do %>
195
- <%= f.action :submit %>
196
- <% end %>
197
- <% end %>
198
- ```
199
-
200
- You can create forms for nested resources:
201
-
202
- ```erb
203
- <%= semantic_form_for [@author, @post] do |f| %>
204
- ```
205
-
206
- Nested forms are also supported (don't forget your models need to be setup correctly with `accepts_nested_attributes_for`). You can do it in the Rails way:
207
-
208
- ```erb
209
- <%= semantic_form_for @post do |f| %>
210
- <%= f.inputs :title, :body, :created_at %>
211
- <%= f.semantic_fields_for :author do |author| %>
212
- <%= author.inputs :first_name, :last_name, :name => "Author" %>
213
- <% end %>
214
- <%= f.actions %>
215
- <% end %>
216
- ```
217
-
218
- Or the Formtastic way with the `:for` option:
219
-
220
- ```erb
221
- <%= semantic_form_for @post do |f| %>
222
- <%= f.inputs :title, :body, :created_at %>
223
- <%= f.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
224
- <%= f.actions %>
225
- <% end %>
226
- ```
227
-
228
- When working in has many association, you can even supply `"%i"` in your fieldset name; they will be properly interpolated with the child index. For example:
229
-
230
- ```erb
231
- <%= semantic_form_for @post do |f| %>
232
- <%= f.inputs %>
233
- <%= f.inputs :name => 'Category #%i', :for => :categories %>
234
- <%= f.actions %>
235
- <% end %>
236
- ```
237
-
238
- Alternatively, the current index can be accessed via the `inputs` block's arguments for use anywhere:
239
-
240
- ```erb
241
- <%= semantic_form_for @post do |f| %>
242
- <%= f.inputs :for => :categories do |category, i| %>
243
- ...
244
- <%= f.actions %>
245
- <% end %>
246
- ```
247
-
248
- If you have more than one form on the same page, it may lead to HTML invalidation because of the way HTML element id attributes are assigned. You can provide a namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generate HTML id. For example:
249
-
250
- ```erb
251
- <%= semantic_form_for(@post, :namespace => 'cat_form') do |f| %>
252
- <%= f.inputs do %>
253
- <%= f.input :title %> # id="cat_form_post_title"
254
- <%= f.input :body %> # id="cat_form_post_body"
255
- <%= f.input :created_at %> # id="cat_form_post_created_at"
256
- <% end %>
257
- <%= f.actions %>
258
- <% end %>
259
- ```
260
-
261
- Customize HTML attributes for any input using the `:input_html` option. Typically this is used to disable the input, change the size of a text field, change the rows in a textarea, or even to add a special class to an input to attach special behavior like [autogrow](http://plugins.jquery.com/project/autogrowtextarea) textareas:
262
-
263
- ```erb
264
- <%= semantic_form_for @post do |f| %>
265
- <%= f.inputs do %>
266
- <%= f.input :title, :input_html => { :size => 10 } %>
267
- <%= f.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 } %>
268
- <%= f.input :created_at, :input_html => { :disabled => true } %>
269
- <%= f.input :updated_at, :input_html => { :readonly => true } %>
270
- <% end %>
271
- <%= f.actions %>
272
- <% end %>
273
- ```
274
-
275
- The same can be done for actions with the `:button_html` option:
276
-
277
- ```erb
278
- <%= semantic_form_for @post do |f| %>
279
- ...
280
- <%= f.actions do %>
281
- <%= f.action :submit, :button_html => { :class => "primary", :disable_with => 'Wait...' } %>
282
- <% end %>
283
- <% end %>
284
- ```
285
-
286
- Customize the HTML attributes for the `<li>` wrapper around every input with the `:wrapper_html` option hash. There's one special key in the hash: (`:class`), which will actually _append_ your string of classes to the existing classes provided by Formtastic (like `"required string error"`).
287
-
288
- ```erb
289
- <%= semantic_form_for @post do |f| %>
290
- <%= f.inputs do %>
291
- <%= f.input :title, :wrapper_html => { :class => "important" } %>
292
- <%= f.input :body %>
293
- <%= f.input :description, :wrapper_html => { :style => "display:none;" } %>
294
- <% end %>
295
- ...
296
- <% end %>
297
- ```
298
-
299
- Many inputs provide a collection of options to choose from (like `:select`, `:radio`, `:check_boxes`, `:boolean`). In many cases, Formtastic can find choices through the model associations, but if you want to use your own set of choices, the `:collection` option is what you want. You can pass in an Array of objects, an array of Strings, a Hash... Throw almost anything at it! Examples:
300
-
301
- ```ruby
302
- f.input :authors, :as => :check_boxes, :collection => User.order("last_name ASC").all
303
- f.input :authors, :as => :check_boxes, :collection => current_user.company.users.active
304
- f.input :authors, :as => :check_boxes, :collection => [@justin, @kate]
305
- f.input :authors, :as => :check_boxes, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
306
- f.input :author, :as => :select, :collection => Author.all
307
- f.input :author, :as => :select, :collection => Author.pluck(:first_name, :id)
308
- f.input :author, :as => :select, :collection => Author.pluck(Arel.sql("CONCAT(`first_name`, ' ', `last_name`)"), :id)
309
- f.input :author, :as => :select, :collection => Author.your_custom_scope_or_class_method
310
- f.input :author, :as => :select, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
311
- f.input :author, :as => :select, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
312
- f.input :author, :as => :radio, :collection => User.all
313
- f.input :author, :as => :radio, :collection => [@justin, @kate]
314
- f.input :author, :as => :radio, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
315
- f.input :author, :as => :radio, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
316
- f.input :admin, :as => :radio, :collection => ["Yes!", "No"]
317
- f.input :book_id, :as => :select, :collection => Hash[Book.all.map{|b| [b.name,b.id]}]
318
- f.input :fav_book,:as => :datalist , :collection => Book.pluck(:name)
319
- ```
320
-
321
-
322
- ## The Available Inputs
323
-
324
- The Formtastic input types:
325
-
326
- * `:select` - a select menu. Default for ActiveRecord associations: `belongs_to`, `has_many`, and `has_and_belongs_to_many`.
327
- * `:check_boxes` - a set of check_box inputs. Alternative to `:select` for ActiveRecord-associations: `has_many`, and has_and_belongs_to_many`.
328
- * `:radio` - a set of radio inputs. Alternative to `:select` for ActiveRecord-associations: `belongs_to`.
329
- * `:time_zone` - a select input. Default for column types: `:string` with name matching `"time_zone"`.
330
- * `:password` - a password input. Default for column types: `:string` with name matching `"password"`.
331
- * `:text` - a textarea. Default for column types: `:text`.
332
- * `:date_select` - a date select. Default for column types: `:date`.
333
- * `:datetime_select` - a date and time select. Default for column types: `:datetime` and `:timestamp`.
334
- * `:time_select` - a time select. Default for column types: `:time`.
335
- * `:boolean` - a checkbox. Default for column types: `:boolean`.
336
- * `:string` - a text field. Default for column types: `:string`.
337
- * `:number` - a text field (just like string). Default for column types: `:integer`, `:float`, and `:decimal`.
338
- * `:file` - a file field. Default for file-attachment attributes matching: [paperclip](http://github.com/thoughtbot/paperclip) or [attachment_fu](http://github.com/technoweenie/attachment_fu).
339
- * `:country@ - a select menu of country names. Default for column types: :string with name `"country"` - requires a *country_select* plugin to be installed.
340
- * `:email` - a text field (just like string). Default for columns with name matching `"email"`. New in HTML5. Works on some mobile browsers already.
341
- * `:url` - a text field (just like string). Default for columns with name matching `"url"`. New in HTML5. Works on some mobile browsers already.
342
- * `:phone` - a text field (just like string). Default for columns with name matching `"phone"` or `"fax"`. New in HTML5.
343
- * `:search` - a text field (just like string). Default for columns with name matching `"search"`. New in HTML5. Works on Safari.
344
- * `:hidden` - a hidden field. Creates a hidden field (added for compatibility).
345
- * `:range` - a slider field.
346
- * `:datalist` - a text field with a accompanying [datalist tag](https://developer.mozilla.org/en/docs/Web/HTML/Element/datalist) which provides options for autocompletion
347
-
348
- The comments in the code are pretty good for each of these (what it does, what the output is, what the options are, etc.) so go check it out.
349
-
350
-
351
- ## Delegation for label lookups
352
-
353
- Formtastic decides which label to use in the following order:
354
-
355
- ```
356
- 1. :label # :label => "Choose Title"
357
- 2. Formtastic i18n # if either :label => true || i18n_lookups_by_default = true (see Internationalization)
358
- 3. Activerecord i18n # if localization file found for the given attribute
359
- 4. label_str_method # if nothing provided this defaults to :humanize but can be set to a custom method
360
- ```
361
-
362
- ## Internationalization (I18n)
363
-
364
- ### Basic Localization
365
-
366
- Formtastic has some neat I18n-features. ActiveRecord object names and attributes are, by default, taken from calling `@object.human_name` and `@object.human_attribute_name(attr)` respectively. There are a few words specific to Formtastic that can be translated. See `lib/locale/en.yml` for more information.
367
-
368
- Basic localization (labels only, with ActiveRecord):
369
-
370
- ```erb
371
- <%= semantic_form_for @post do |f| %>
372
- <%= f.inputs do %>
373
- <%= f.input :title %> # => :label => I18n.t('activerecord.attributes.user.title') or 'Title'
374
- <%= f.input :body %> # => :label => I18n.t('activerecord.attributes.user.body') or 'Body'
375
- <%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
376
- <% end %>
377
- <% end %>
378
- ```
379
-
380
- *Note:* This is perfectly fine if you just want your labels/attributes and/or models to be translated using *ActiveRecord I18n attribute translations*, and you don't use input hints and legends. But what if you do? And what if you don't want same labels in all forms?
381
-
382
- ### Enhanced Localization (Formtastic I18n API)
383
-
384
- Formtastic supports localized *labels*, *hints*, *legends*, *actions* using the I18n API for more advanced usage. Your forms can now be DRYer and more flexible than ever, and still fully localized. This is how:
385
-
386
- *1. Enable I18n lookups by default (`config/initializers/formtastic.rb`):*
387
-
388
- ```ruby
389
- Formtastic::FormBuilder.i18n_lookups_by_default = true
390
- ```
391
-
392
- *2. Add some label-translations/variants (`config/locales/en.yml`):*
393
-
394
- ```yml
395
- en:
396
- formtastic:
397
- titles:
398
- post_details: "Post details"
399
- labels:
400
- post:
401
- title: "Your Title"
402
- body: "Write something..."
403
- edit:
404
- title: "Edit title"
405
- hints:
406
- post:
407
- title: "Choose a good title for your post."
408
- body: "Write something inspiring here."
409
- placeholders:
410
- post:
411
- title: "Title your post"
412
- slug: "Leave blank for an automatically generated slug"
413
- user:
414
- email: "you@yours.com"
415
- actions:
416
- create: "Create my %{model}"
417
- update: "Save changes"
418
- reset: "Reset form"
419
- cancel: "Cancel and go back"
420
- dummie: "Launch!"
421
- ```
422
-
423
- *3. ...and now you'll get:*
424
-
425
- ```erb
426
- <%= semantic_form_for Post.new do |f| %>
427
- <%= f.inputs do %>
428
- <%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
429
- <%= f.input :body %> # => :label => "Write something...", :hint => "Write something inspiring here."
430
- <%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
431
- <% end %>
432
- <%= f.actions do %>
433
- <%= f.action :submit %> # => "Create my %{model}"
434
- <% end %>
435
- <% end %>
436
- ```
437
-
438
- *4. Localized titles (a.k.a. legends):*
439
-
440
- _Note: Slightly different because Formtastic can't guess how you group fields in a form. Legend text can be set with first (as in the sample below) specified value, or :name/:title options - depending on what flavor is preferred._
441
-
442
- ```erb
443
- <%= semantic_form_for @post do |f| %>
444
- <%= f.inputs :post_details do %> # => :title => "Post details"
445
- # ...
446
- <% end %>
447
- # ...
448
- <% end %>
449
- ```
450
-
451
- *5. Override I18n settings:*
452
-
453
- ```erb
454
- <%= semantic_form_for @post do |f| %>
455
- <%= f.inputs do %>
456
- <%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
457
- <%= f.input :body, :hint => false %> # => :label => "Write something..."
458
- <%= f.input :section, :label => 'Some section' %> # => :label => 'Some section'
459
- <% end %>
460
- <%= f.actions do %>
461
- <%= f.action :submit, :label => :dummie %> # => "Launch!"
462
- <% end %>
463
- <% end %>
464
- ```
465
-
466
- If I18n-lookups is disabled, i.e.:
467
-
468
- ```ruby
469
- Formtastic::FormBuilder.i18n_lookups_by_default = false
470
- ```
471
-
472
- ...then you can enable I18n within the forms instead:
473
-
474
- ```erb
475
- <%= semantic_form_for @post do |f| %>
476
- <%= f.inputs do %>
477
- <%= f.input :title, :label => true %> # => :label => "Choose a title..."
478
- <%= f.input :body, :label => true %> # => :label => "Write something..."
479
- <%= f.input :section, :label => true %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
480
- <% end %>
481
- <%= f.actions do %>
482
- <%= f.action :submit, :label => true %> # => "Update %{model}" (if we are in edit that is...)
483
- <% end %>
484
- <% end %>
485
- ```
486
-
487
- *6. Advanced I18n lookups*
488
-
489
- For more flexible forms; Formtastic finds translations using a bottom-up approach taking the following variables in account:
490
-
491
- * `MODEL`, e.g. "post"
492
- * `ACTION`, e.g. "edit"
493
- * `KEY/ATTRIBUTE`, e.g. "title", :my_custom_key, ...
494
-
495
- ...in the following order:
496
-
497
- 1. `formtastic.{titles,labels,hints,actions}.MODEL.ACTION.ATTRIBUTE` - by model and action
498
- 2. `formtastic.{titles,labels,hints,actions}.MODEL.ATTRIBUTE` - by model
499
- 3. `formtastic.{titles,labels,hints,actions}.ATTRIBUTE` - global default
500
-
501
- ...which means that you can define translations like this:
502
-
503
- ```yml
504
- en:
505
- formtastic:
506
- labels:
507
- title: "Title" # Default global value
508
- article:
509
- body: "Article content"
510
- post:
511
- new:
512
- title: "Choose a title..."
513
- body: "Write something..."
514
- edit:
515
- title: "Edit title"
516
- body: "Edit body"
517
- ```
518
-
519
- Values for `labels`/`hints`/`actions` are can take values: `String` (explicit value), `Symbol` (i18n-lookup-key relative to the current "type", e.g. actions:), `true` (force I18n lookup), `false` (force no I18n lookup). Titles (legends) can only take: `String` and `Symbol` - true/false have no meaning.
520
-
521
- *7. Basic Translations*
522
- If you want some basic translations, take a look on the [formtastic_i18n gem](https://github.com/timoschilling/formtastic_i18n).
523
-
524
- ## Semantic errors
525
-
526
- You can show errors on base (by default) and any other attribute just by passing its name to the semantic_errors method:
527
-
528
- ```erb
529
- <%= semantic_form_for @post do |f| %>
530
- <%= f.semantic_errors :state %>
531
- <% end %>
532
- ```
533
-
534
-
535
- ## Modified & Custom Inputs
536
-
537
- You can modify existing inputs, subclass them, or create your own from scratch. Here's the basic process:
538
-
539
- * Run the input generator and provide your custom input name. For example, `rails generate formtastic:input hat_size`. This creates the file `app/inputs/hat_size_input.rb`. You can also provide namespace to input name like `rails generate formtastic:input foo/custom` or `rails generate formtastic:input Foo::Custom`, this will create the file `app/inputs/foo/custom_input.rb` in both cases.
540
- * To use that input, leave off the word "input" in your `as` statement. For example, `f.input(:size, :as => :hat_size)`
541
-
542
- Specific examples follow.
543
-
544
- ### Changing Existing Input Behavior
545
-
546
- To modify the behavior of `StringInput`, subclass it in a new file, `app/inputs/string_input.rb`:
547
-
548
- ```ruby
549
- class StringInput < Formtastic::Inputs::StringInput
550
- def to_html
551
- puts "this is my modified version of StringInput"
552
- super
553
- end
554
- end
555
- ```
556
-
557
- Another way to modify behavior is by using the input generator:
558
- ```shell
559
- $ rails generate formtastic:input string --extend
560
- ```
561
-
562
- This generates the file `app/inputs/string_input.rb` with its respective content class.
563
-
564
- You can use your modified version with `:as => :string`.
565
-
566
- ### Creating New Inputs Based on Existing Ones
567
-
568
- To create your own new types of inputs based on existing inputs, the process is similar. For example, to create `FlexibleTextInput` based on `StringInput`, put the following in `app/inputs/flexible_text_input.rb`:
569
-
570
- ```ruby
571
- class FlexibleTextInput < Formtastic::Inputs::StringInput
572
- def input_html_options
573
- super.merge(:class => "flexible-text-area")
574
- end
575
- end
576
- ```
577
-
578
- You can also extend existing input behavior by using the input generator:
579
-
580
- ```shell
581
- $ rails generate formtastic:input FlexibleText --extend string
582
- ```
583
-
584
- This generates the file `app/inputs/flexible_text_input.rb` with its respective content class.
585
-
586
- You can use your new input with `:as => :flexible_text`.
587
-
588
- ### Creating New Inputs From Scratch
589
-
590
- To create a custom `DatePickerInput` from scratch, put the following in `app/inputs/date_picker_input.rb`:
591
-
592
- ```ruby
593
- class DatePickerInput
594
- include Formtastic::Inputs::Base
595
- def to_html
596
- # ...
597
- end
598
- end
599
- ```
600
-
601
- You can use your new input with `:as => :date_picker`.
602
-
603
-
604
- ## Dependencies
605
-
606
- There are none other than Rails itself, but...
607
-
608
- * If you want to use the `:country` input, you'll need to install the [country-select plugin](https://github.com/stefanpenner/country_select) (or any other country_select plugin with the same API). Both 1.x and 2.x are supported, but they behave differently when storing data, so please see their [upgrade notes](https://github.com/stefanpenner/country_select/blob/master/UPGRADING.md) if switching from 1.x.
609
- * There are a bunch of development dependencies if you plan to contribute to Formtastic
610
-
611
-
612
- ## How to contribute
613
-
614
- * Fork the project on Github
615
- * Install development dependencies (`bundle install` and `appraisal install`)
616
- * Create a topic branch for your changes
617
- * Ensure that you provide *documentation* and *test coverage* for your changes (patches won't be accepted without)
618
- * Ensure that all tests pass (`bundle exec rake`)
619
- * Create a pull request on Github (these are also a great place to start a conversation around a patch as early as possible)
620
-
621
-
622
- ## Project Info
623
-
624
- Formtastic was created by [Justin French](http://www.justinfrench.com) with contributions from around 180 awesome developers. Run `git shortlog -n -s` to see the awesome.
625
-
626
- The project is hosted on Github: [http://github.com/justinfrench/formtastic](http://github.com/justinfrench/formtastic), where your contributions, forkings, comments, issues and feedback are greatly welcomed.
627
-
628
- Copyright (c) 2007-2016 Justin French, released under the MIT license.
629
-
1
+ # Formtastic
2
+
3
+ [![Build Status](https://github.com/formtastic/formtastic/workflows/test/badge.svg)](https://github.com/formtastic/formtastic/actions)
4
+ [![Inline docs](https://inch-ci.org/github/justinfrench/formtastic.svg?branch=master)](https://inch-ci.org/github/justinfrench/formtastic)
5
+ [![Code Climate](https://codeclimate.com/github/formtastic/formtastic/badges/gpa.svg)](https://codeclimate.com/github/formtastic/formtastic)
6
+ [![Gem Version](https://badge.fury.io/rb/formtastic.svg)](https://badge.fury.io/rb/formtastic)
7
+
8
+ Formtastic is a Rails FormBuilder DSL (with some other goodies) to make it far easier to create beautiful, semantically rich, syntactically awesome, readily stylable and wonderfully accessible HTML forms in your Rails applications.
9
+
10
+ ## Documentation & Support
11
+
12
+ * [Documentation is available on rdoc.info](https://rdoc.info/projects/formtastic/formtastic)
13
+ * [We track issues & bugs on GitHub](https://github.com/formtastic/formtastic/issues)
14
+ * [We have a wiki on GitHub](https://github.com/formtastic/formtastic/wiki)
15
+ * [StackOverflow can help](https://stackoverflow.com/questions/tagged/formtastic)
16
+
17
+ ## Compatibility
18
+
19
+ * Formtastic 4 requires Rails 5.2 and Ruby 2.5 minimum
20
+ * Formtastic 3 requires Rails 3.2.13 minimum
21
+ * Formtastic 2 requires Rails 3
22
+ * Formtastic, much like Rails, is very ActiveRecord-centric. Many are successfully using other ActiveModel-like ORMs and objects (DataMapper, MongoMapper, Mongoid, Authlogic, Devise...) but we're not guaranteeing full compatibility at this stage. Patches are welcome!
23
+
24
+ ## The Story
25
+
26
+ One day, I finally had enough, so I opened up my text editor, and wrote a DSL for how I'd like to author forms:
27
+
28
+ ```erb
29
+ <%= semantic_form_for @article do |f| %>
30
+
31
+ <%= f.inputs :name => "Basic" do %>
32
+ <%= f.input :title %>
33
+ <%= f.input :body %>
34
+ <%= f.input :section %>
35
+ <%= f.input :publication_state, :as => :radio %>
36
+ <%= f.input :category %>
37
+ <%= f.input :allow_comments, :label => "Allow commenting on this article" %>
38
+ <% end %>
39
+
40
+ <%= f.inputs :name => "Advanced" do %>
41
+ <%= f.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
42
+ <%= f.input :extract, :required => false %>
43
+ <%= f.input :description, :required => false %>
44
+ <%= f.input :url_title, :required => false %>
45
+ <% end %>
46
+
47
+ <%= f.inputs :name => "Author", :for => :author do |author_form| %>
48
+ <%= author_form.input :first_name %>
49
+ <%= author_form.input :last_name %>
50
+ <% end %>
51
+
52
+ <%= f.actions do %>
53
+ <%= f.action :submit, :as => :button %>
54
+ <%= f.action :cancel, :as => :link %>
55
+ <% end %>
56
+
57
+ <% end %>
58
+ ```
59
+
60
+ I also wrote the accompanying HTML output I expected, favoring something very similar to the fieldsets, lists and other semantic elements Aaron Gustafson presented in [Learning to Love Forms](https://www.slideshare.net/AaronGustafson/learning-to-love-forms-webvisions-07), hacking together enough Ruby to prove it could be done.
61
+
62
+
63
+ ## It's awesome because...
64
+
65
+ * It can handle `belongs_to` associations (like Post belongs_to :author), rendering a select or set of radio inputs with choices from the parent model.
66
+ * It can handle `has_many` and `has_and_belongs_to_many` associations (like: Post has_many :tags), rendering a multi-select with choices from the child models.
67
+ * It's Rails 3/4 compatible (including nested forms).
68
+ * It has internationalization (I18n)!
69
+ * It's _really_ quick to get started with a basic form in place (4 lines), then go back to add in more detail if you need it.
70
+ * There's heaps of elements, id and class attributes for you to hook in your CSS and JS.
71
+ * It handles real world stuff like inline hints, inline error messages & help text.
72
+ * It doesn't hijack or change any of the standard Rails form inputs, so you can still use them as expected (even mix and match).
73
+ * It's got absolutely awesome spec coverage.
74
+ * There's a bunch of people using and working on it (it's not just one developer building half a solution).
75
+ * It has growing HTML5 support (new inputs like email/phone/search, new attributes like required/min/max/step/placeholder)
76
+
77
+
78
+ ## Opinions
79
+
80
+ * It should be easier to do things the right way than the wrong way.
81
+ * Sometimes _more mark-up_ is better.
82
+ * Elements and attribute hooks are _gold_ for stylesheet authors.
83
+ * Make the common things we do easy, yet ensure uncommon things are still possible.
84
+
85
+
86
+ ## Installation
87
+
88
+ Simply add Formtastic to your Gemfile and bundle it up:
89
+
90
+ ```ruby
91
+ gem 'formtastic', '~> 4.0'
92
+ ```
93
+
94
+ Run the installation generator:
95
+
96
+ ```shell
97
+ $ rails generate formtastic:install
98
+ ```
99
+
100
+
101
+ ## Stylesheets
102
+
103
+ A proof-of-concept set of stylesheets are provided which you can include in your layout. Customization is best achieved by overriding these styles in an additional stylesheet.
104
+
105
+ Rails 3.1 introduces an asset pipeline that allows plugins like Formtastic to serve their own Stylesheets, Javascripts, etc without having to run generators that copy them across to the host application. Formtastic makes three stylesheets available as an Engine, you just need to require them in your global stylesheets.
106
+
107
+ ```css
108
+ # app/assets/stylesheets/application.css
109
+ *= require formtastic
110
+ *= require my_formtastic_changes
111
+ ```
112
+
113
+ Conditional stylesheets need to be compiled separately to prevent them being bundled and included with other application styles. Remove `require_tree .` from application.css and specify required stylesheets individually.
114
+
115
+ ```css
116
+ # app/assets/stylesheets/ie6.css
117
+ *= require formtastic_ie6
118
+
119
+ # app/assets/stylesheets/ie7.css
120
+ *= require formtastic_ie7
121
+ ```
122
+
123
+ ```erb
124
+ # app/views/layouts/application.html.erb
125
+ <%= stylesheet_link_tag 'application' %>
126
+ <!--[if IE 6]><%= stylesheet_link_tag 'ie6' %><![endif]-->
127
+ <!--[if IE 7]><%= stylesheet_link_tag 'ie7' %><![endif]-->
128
+ ```
129
+
130
+ ```ruby
131
+ # config/environments/production.rb
132
+ config.assets.precompile += %w( ie6.css ie7.css )
133
+ ```
134
+
135
+ ## Usage
136
+
137
+ Forms are really boring to code... you want to get onto the good stuff as fast as possible.
138
+
139
+ This renders a set of inputs (one for _most_ columns in the database table, and one for each ActiveRecord `belongs_to`-association), followed by default action buttons (an input submit button):
140
+
141
+ ```erb
142
+ <%= semantic_form_for @user do |f| %>
143
+ <%= f.inputs %>
144
+ <%= f.actions %>
145
+ <% end %>
146
+ ```
147
+
148
+ This is a great way to get something up fast, but like scaffolding, it's *not recommended for production*. Don't be so lazy!
149
+
150
+ To specify the order of the fields, skip some of the fields or even add in fields that Formtastic couldn't infer. You can pass in a list of field names to `inputs` and list of action names to `actions`:
151
+
152
+ ```erb
153
+ <%= semantic_form_for @user do |f| %>
154
+ <%= f.inputs :title, :body, :section, :categories, :created_at %>
155
+ <%= f.actions :submit, :cancel %>
156
+ <% end %>
157
+ ```
158
+
159
+ You probably want control over the input type Formtastic uses for each field. You can expand the `inputs` and `actions` to block helper format and use the `:as` option to specify an exact input type:
160
+
161
+ ```erb
162
+ <%= semantic_form_for @post do |f| %>
163
+ <%= f.inputs do %>
164
+ <%= f.input :title %>
165
+ <%= f.input :body %>
166
+ <%= f.input :section, :as => :radio %>
167
+ <%= f.input :categories %>
168
+ <%= f.input :created_at, :as => :string %>
169
+ <% end %>
170
+ <%= f.actions do %>
171
+ <%= f.action :submit, :as => :button %>
172
+ <%= f.action :cancel, :as => :link %>
173
+ <% end %>
174
+ <% end %>
175
+ ```
176
+
177
+ If you want to customize the label text, or render some hint text below the field, specify which fields are required/optional, or break the form into two fieldsets, the DSL is pretty comprehensive:
178
+
179
+ ```erb
180
+ <%= semantic_form_for @post do |f| %>
181
+ <%= f.inputs "Basic", :id => "basic" do %>
182
+ <%= f.input :title %>
183
+ <%= f.input :body %>
184
+ <% end %>
185
+ <%= f.inputs :name => "Advanced Options", :id => "advanced" do %>
186
+ <%= f.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
187
+ <%= f.input :section, :as => :radio %>
188
+ <%= f.input :user, :label => "Author" %>
189
+ <%= f.input :categories, :required => false %>
190
+ <%= f.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
191
+ <% end %>
192
+ <%= f.actions do %>
193
+ <%= f.action :submit %>
194
+ <% end %>
195
+ <% end %>
196
+ ```
197
+
198
+ You can create forms for nested resources:
199
+
200
+ ```erb
201
+ <%= semantic_form_for [@author, @post] do |f| %>
202
+ ```
203
+
204
+ Nested forms are also supported (don't forget your models need to be setup correctly with `accepts_nested_attributes_for`). You can do it in the Rails way:
205
+
206
+ ```erb
207
+ <%= semantic_form_for @post do |f| %>
208
+ <%= f.inputs :title, :body, :created_at %>
209
+ <%= f.semantic_fields_for :author do |author| %>
210
+ <%= author.inputs :first_name, :last_name, :name => "Author" %>
211
+ <% end %>
212
+ <%= f.actions %>
213
+ <% end %>
214
+ ```
215
+
216
+ Or the Formtastic way with the `:for` option:
217
+
218
+ ```erb
219
+ <%= semantic_form_for @post do |f| %>
220
+ <%= f.inputs :title, :body, :created_at %>
221
+ <%= f.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
222
+ <%= f.actions %>
223
+ <% end %>
224
+ ```
225
+
226
+ When working in has many association, you can even supply `"%i"` in your fieldset name; they will be properly interpolated with the child index. For example:
227
+
228
+ ```erb
229
+ <%= semantic_form_for @post do |f| %>
230
+ <%= f.inputs %>
231
+ <%= f.inputs :name => 'Category #%i', :for => :categories %>
232
+ <%= f.actions %>
233
+ <% end %>
234
+ ```
235
+
236
+ Alternatively, the current index can be accessed via the `inputs` block's arguments for use anywhere:
237
+
238
+ ```erb
239
+ <%= semantic_form_for @post do |f| %>
240
+ <%= f.inputs :for => :categories do |category, i| %>
241
+ ...
242
+ <%= f.actions %>
243
+ <% end %>
244
+ ```
245
+
246
+ If you have more than one form on the same page, it may lead to HTML invalidation because of the way HTML element id attributes are assigned. You can provide a namespace for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generate HTML id. For example:
247
+
248
+ ```erb
249
+ <%= semantic_form_for(@post, :namespace => 'cat_form') do |f| %>
250
+ <%= f.inputs do %>
251
+ <%= f.input :title %> # id="cat_form_post_title"
252
+ <%= f.input :body %> # id="cat_form_post_body"
253
+ <%= f.input :created_at %> # id="cat_form_post_created_at"
254
+ <% end %>
255
+ <%= f.actions %>
256
+ <% end %>
257
+ ```
258
+
259
+ Customize HTML attributes for any input using the `:input_html` option. Typically this is used to disable the input, change the size of a text field, change the rows in a textarea, or even to add a special class to an input to attach special behavior like [autogrow](https://plugins.jquery.com/project/autogrowtextarea) textareas:
260
+
261
+ ```erb
262
+ <%= semantic_form_for @post do |f| %>
263
+ <%= f.inputs do %>
264
+ <%= f.input :title, :input_html => { :size => 10 } %>
265
+ <%= f.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 } %>
266
+ <%= f.input :created_at, :input_html => { :disabled => true } %>
267
+ <%= f.input :updated_at, :input_html => { :readonly => true } %>
268
+ <% end %>
269
+ <%= f.actions %>
270
+ <% end %>
271
+ ```
272
+
273
+ The same can be done for actions with the `:button_html` option:
274
+
275
+ ```erb
276
+ <%= semantic_form_for @post do |f| %>
277
+ ...
278
+ <%= f.actions do %>
279
+ <%= f.action :submit, :button_html => { :class => "primary", :disable_with => 'Wait...' } %>
280
+ <% end %>
281
+ <% end %>
282
+ ```
283
+
284
+ Customize the HTML attributes for the `<li>` wrapper around every input with the `:wrapper_html` option hash. There's one special key in the hash: (`:class`), which will actually _append_ your string of classes to the existing classes provided by Formtastic (like `"required string error"`).
285
+
286
+ ```erb
287
+ <%= semantic_form_for @post do |f| %>
288
+ <%= f.inputs do %>
289
+ <%= f.input :title, :wrapper_html => { :class => "important" } %>
290
+ <%= f.input :body %>
291
+ <%= f.input :description, :wrapper_html => { :style => "display:none;" } %>
292
+ <% end %>
293
+ ...
294
+ <% end %>
295
+ ```
296
+
297
+ Many inputs provide a collection of options to choose from (like `:select`, `:radio`, `:check_boxes`, `:boolean`). In many cases, Formtastic can find choices through the model associations, but if you want to use your own set of choices, the `:collection` option is what you want. You can pass in an Array of objects, an array of Strings, a Hash... Throw almost anything at it! Examples:
298
+
299
+ ```ruby
300
+ f.input :authors, :as => :check_boxes, :collection => User.order("last_name ASC").all
301
+ f.input :authors, :as => :check_boxes, :collection => current_user.company.users.active
302
+ f.input :authors, :as => :check_boxes, :collection => [@justin, @kate]
303
+ f.input :authors, :as => :check_boxes, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
304
+ f.input :author, :as => :select, :collection => Author.all
305
+ f.input :author, :as => :select, :collection => Author.pluck(:first_name, :id)
306
+ f.input :author, :as => :select, :collection => Author.pluck(Arel.sql("CONCAT(`first_name`, ' ', `last_name`)"), :id)
307
+ f.input :author, :as => :select, :collection => Author.your_custom_scope_or_class_method
308
+ f.input :author, :as => :select, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
309
+ f.input :author, :as => :select, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
310
+ f.input :author, :as => :radio, :collection => User.all
311
+ f.input :author, :as => :radio, :collection => [@justin, @kate]
312
+ f.input :author, :as => :radio, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
313
+ f.input :author, :as => :radio, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
314
+ f.input :admin, :as => :radio, :collection => ["Yes!", "No"]
315
+ f.input :book_id, :as => :select, :collection => Hash[Book.all.map{|b| [b.name,b.id]}]
316
+ f.input :fav_book,:as => :datalist , :collection => Book.pluck(:name)
317
+ ```
318
+
319
+
320
+ ## The Available Inputs
321
+
322
+ The Formtastic input types:
323
+
324
+ * `:select` - a select menu. Default for ActiveRecord associations: `belongs_to`, `has_many`, and `has_and_belongs_to_many`.
325
+ * `:check_boxes` - a set of check_box inputs. Alternative to `:select` for ActiveRecord-associations: `has_many`, and has_and_belongs_to_many`.
326
+ * `:radio` - a set of radio inputs. Alternative to `:select` for ActiveRecord-associations: `belongs_to`.
327
+ * `:time_zone` - a select input. Default for column types: `:string` with name matching `"time_zone"`.
328
+ * `:password` - a password input. Default for column types: `:string` with name matching `"password"`.
329
+ * `:text` - a textarea. Default for column types: `:text`.
330
+ * `:date_select` - a date select. Default for column types: `:date`.
331
+ * `:datetime_select` - a date and time select. Default for column types: `:datetime` and `:timestamp`.
332
+ * `:time_select` - a time select. Default for column types: `:time`.
333
+ * `:boolean` - a checkbox. Default for column types: `:boolean`.
334
+ * `:string` - a text field. Default for column types: `:string`.
335
+ * `:number` - a text field (just like string). Default for column types: `:integer`, `:float`, and `:decimal`.
336
+ * `:file` - a file field. Default for file-attachment attributes matching: [paperclip](https://github.com/thoughtbot/paperclip) or [attachment_fu](https://github.com/technoweenie/attachment_fu).
337
+ * `:country` - a select menu of country names. Default for column types: `:string` with name `"country"` - requires a *country_select* plugin to be installed.
338
+ * `:email` - a text field (just like string). Default for columns with name matching `"email"`. New in HTML5. Works on some mobile browsers already.
339
+ * `:url` - a text field (just like string). Default for columns with name matching `"url"`. New in HTML5. Works on some mobile browsers already.
340
+ * `:phone` - a text field (just like string). Default for columns with name matching `"phone"` or `"fax"`. New in HTML5.
341
+ * `:search` - a text field (just like string). Default for columns with name matching `"search"`. New in HTML5. Works on Safari.
342
+ * `:hidden` - a hidden field. Creates a hidden field (added for compatibility).
343
+ * `:range` - a slider field.
344
+ * `:datalist` - a text field with a accompanying [datalist tag](https://developer.mozilla.org/en/docs/Web/HTML/Element/datalist) which provides options for autocompletion
345
+
346
+ The comments in the code are pretty good for each of these (what it does, what the output is, what the options are, etc.) so go check it out.
347
+
348
+
349
+ ## Delegation for label lookups
350
+
351
+ Formtastic decides which label to use in the following order:
352
+
353
+ ```
354
+ 1. :label # :label => "Choose Title"
355
+ 2. Formtastic i18n # if either :label => true || i18n_lookups_by_default = true (see Internationalization)
356
+ 3. Activerecord i18n # if localization file found for the given attribute
357
+ 4. label_str_method # if nothing provided this defaults to :humanize but can be set to a custom method
358
+ ```
359
+
360
+ ## Internationalization (I18n)
361
+
362
+ ### Basic Localization
363
+
364
+ Formtastic has some neat I18n-features. ActiveRecord object names and attributes are, by default, taken from calling `@object.human_name` and `@object.human_attribute_name(attr)` respectively. There are a few words specific to Formtastic that can be translated. See `lib/locale/en.yml` for more information.
365
+
366
+ Basic localization (labels only, with ActiveRecord):
367
+
368
+ ```erb
369
+ <%= semantic_form_for @post do |f| %>
370
+ <%= f.inputs do %>
371
+ <%= f.input :title %> # => :label => I18n.t('activerecord.attributes.user.title') or 'Title'
372
+ <%= f.input :body %> # => :label => I18n.t('activerecord.attributes.user.body') or 'Body'
373
+ <%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
374
+ <% end %>
375
+ <% end %>
376
+ ```
377
+
378
+ *Note:* This is perfectly fine if you just want your labels/attributes and/or models to be translated using *ActiveRecord I18n attribute translations*, and you don't use input hints and legends. But what if you do? And what if you don't want same labels in all forms?
379
+
380
+ ### Enhanced Localization (Formtastic I18n API)
381
+
382
+ Formtastic supports localized *labels*, *hints*, *legends*, *actions* using the I18n API for more advanced usage. Your forms can now be DRYer and more flexible than ever, and still fully localized. This is how:
383
+
384
+ *1. Enable I18n lookups by default (`config/initializers/formtastic.rb`):*
385
+
386
+ ```ruby
387
+ Formtastic::FormBuilder.i18n_lookups_by_default = true
388
+ ```
389
+
390
+ *2. Add some label-translations/variants (`config/locales/en.yml`):*
391
+
392
+ ```yml
393
+ en:
394
+ formtastic:
395
+ titles:
396
+ post_details: "Post details"
397
+ labels:
398
+ post:
399
+ title: "Your Title"
400
+ body: "Write something..."
401
+ edit:
402
+ title: "Edit title"
403
+ hints:
404
+ post:
405
+ title: "Choose a good title for your post."
406
+ body: "Write something inspiring here."
407
+ placeholders:
408
+ post:
409
+ title: "Title your post"
410
+ slug: "Leave blank for an automatically generated slug"
411
+ user:
412
+ email: "you@yours.com"
413
+ actions:
414
+ create: "Create my %{model}"
415
+ update: "Save changes"
416
+ reset: "Reset form"
417
+ cancel: "Cancel and go back"
418
+ dummie: "Launch!"
419
+ ```
420
+
421
+ *3. ...and now you'll get:*
422
+
423
+ ```erb
424
+ <%= semantic_form_for Post.new do |f| %>
425
+ <%= f.inputs do %>
426
+ <%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
427
+ <%= f.input :body %> # => :label => "Write something...", :hint => "Write something inspiring here."
428
+ <%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
429
+ <% end %>
430
+ <%= f.actions do %>
431
+ <%= f.action :submit %> # => "Create my %{model}"
432
+ <% end %>
433
+ <% end %>
434
+ ```
435
+
436
+ *4. Localized titles (a.k.a. legends):*
437
+
438
+ _Note: Slightly different because Formtastic can't guess how you group fields in a form. Legend text can be set with first (as in the sample below) specified value, or :name/:title options - depending on what flavor is preferred._
439
+
440
+ ```erb
441
+ <%= semantic_form_for @post do |f| %>
442
+ <%= f.inputs :post_details do %> # => :title => "Post details"
443
+ # ...
444
+ <% end %>
445
+ # ...
446
+ <% end %>
447
+ ```
448
+
449
+ *5. Override I18n settings:*
450
+
451
+ ```erb
452
+ <%= semantic_form_for @post do |f| %>
453
+ <%= f.inputs do %>
454
+ <%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
455
+ <%= f.input :body, :hint => false %> # => :label => "Write something..."
456
+ <%= f.input :section, :label => 'Some section' %> # => :label => 'Some section'
457
+ <% end %>
458
+ <%= f.actions do %>
459
+ <%= f.action :submit, :label => :dummie %> # => "Launch!"
460
+ <% end %>
461
+ <% end %>
462
+ ```
463
+
464
+ If I18n-lookups is disabled, i.e.:
465
+
466
+ ```ruby
467
+ Formtastic::FormBuilder.i18n_lookups_by_default = false
468
+ ```
469
+
470
+ ...then you can enable I18n within the forms instead:
471
+
472
+ ```erb
473
+ <%= semantic_form_for @post do |f| %>
474
+ <%= f.inputs do %>
475
+ <%= f.input :title, :label => true %> # => :label => "Choose a title..."
476
+ <%= f.input :body, :label => true %> # => :label => "Write something..."
477
+ <%= f.input :section, :label => true %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
478
+ <% end %>
479
+ <%= f.actions do %>
480
+ <%= f.action :submit, :label => true %> # => "Update %{model}" (if we are in edit that is...)
481
+ <% end %>
482
+ <% end %>
483
+ ```
484
+
485
+ *6. Advanced I18n lookups*
486
+
487
+ For more flexible forms; Formtastic finds translations using a bottom-up approach taking the following variables in account:
488
+
489
+ * `MODEL`, e.g. "post"
490
+ * `ACTION`, e.g. "edit"
491
+ * `KEY/ATTRIBUTE`, e.g. "title", :my_custom_key, ...
492
+
493
+ ...in the following order:
494
+
495
+ 1. `formtastic.{titles,labels,hints,actions}.MODEL.ACTION.ATTRIBUTE` - by model and action
496
+ 2. `formtastic.{titles,labels,hints,actions}.MODEL.ATTRIBUTE` - by model
497
+ 3. `formtastic.{titles,labels,hints,actions}.ATTRIBUTE` - global default
498
+
499
+ ...which means that you can define translations like this:
500
+
501
+ ```yml
502
+ en:
503
+ formtastic:
504
+ labels:
505
+ title: "Title" # Default global value
506
+ article:
507
+ body: "Article content"
508
+ post:
509
+ new:
510
+ title: "Choose a title..."
511
+ body: "Write something..."
512
+ edit:
513
+ title: "Edit title"
514
+ body: "Edit body"
515
+ ```
516
+
517
+ Values for `labels`/`hints`/`actions` are can take values: `String` (explicit value), `Symbol` (i18n-lookup-key relative to the current "type", e.g. actions:), `true` (force I18n lookup), `false` (force no I18n lookup). Titles (legends) can only take: `String` and `Symbol` - true/false have no meaning.
518
+
519
+ *7. Basic Translations*
520
+ If you want some basic translations, take a look on the [formtastic_i18n gem](https://github.com/timoschilling/formtastic_i18n).
521
+
522
+ ## Semantic errors
523
+
524
+ You can show errors on base (by default) and any other attribute just by passing its name to the semantic_errors method:
525
+
526
+ ```erb
527
+ <%= semantic_form_for @post do |f| %>
528
+ <%= f.semantic_errors :state %>
529
+ <% end %>
530
+ ```
531
+
532
+
533
+ ## Modified & Custom Inputs
534
+
535
+ You can modify existing inputs, subclass them, or create your own from scratch. Here's the basic process:
536
+
537
+ * Run the input generator and provide your custom input name. For example, `rails generate formtastic:input hat_size`. This creates the file `app/inputs/hat_size_input.rb`. You can also provide namespace to input name like `rails generate formtastic:input foo/custom` or `rails generate formtastic:input Foo::Custom`, this will create the file `app/inputs/foo/custom_input.rb` in both cases.
538
+ * To use that input, leave off the word "input" in your `as` statement. For example, `f.input(:size, :as => :hat_size)`
539
+
540
+ Specific examples follow.
541
+
542
+ ### Changing Existing Input Behavior
543
+
544
+ To modify the behavior of `StringInput`, subclass it in a new file, `app/inputs/string_input.rb`:
545
+
546
+ ```ruby
547
+ class StringInput < Formtastic::Inputs::StringInput
548
+ def to_html
549
+ puts "this is my modified version of StringInput"
550
+ super
551
+ end
552
+ end
553
+ ```
554
+
555
+ Another way to modify behavior is by using the input generator:
556
+ ```shell
557
+ $ rails generate formtastic:input string --extend
558
+ ```
559
+
560
+ This generates the file `app/inputs/string_input.rb` with its respective content class.
561
+
562
+ You can use your modified version with `:as => :string`.
563
+
564
+ ### Creating New Inputs Based on Existing Ones
565
+
566
+ To create your own new types of inputs based on existing inputs, the process is similar. For example, to create `FlexibleTextInput` based on `StringInput`, put the following in `app/inputs/flexible_text_input.rb`:
567
+
568
+ ```ruby
569
+ class FlexibleTextInput < Formtastic::Inputs::StringInput
570
+ def input_html_options
571
+ super.merge(:class => "flexible-text-area")
572
+ end
573
+ end
574
+ ```
575
+
576
+ You can also extend existing input behavior by using the input generator:
577
+
578
+ ```shell
579
+ $ rails generate formtastic:input FlexibleText --extend string
580
+ ```
581
+
582
+ This generates the file `app/inputs/flexible_text_input.rb` with its respective content class.
583
+
584
+ You can use your new input with `:as => :flexible_text`.
585
+
586
+ ### Creating New Inputs From Scratch
587
+
588
+ To create a custom `DatePickerInput` from scratch, put the following in `app/inputs/date_picker_input.rb`:
589
+
590
+ ```ruby
591
+ class DatePickerInput
592
+ include Formtastic::Inputs::Base
593
+ def to_html
594
+ # ...
595
+ end
596
+ end
597
+ ```
598
+
599
+ You can use your new input with `:as => :date_picker`.
600
+
601
+
602
+ ## Dependencies
603
+
604
+ There are none other than Rails itself, but...
605
+
606
+ * If you want to use the `:country` input, you'll need to install the [country-select plugin](https://github.com/stefanpenner/country_select) (or any other country_select plugin with the same API). Both 1.x and 2.x are supported, but they behave differently when storing data, so please see their [upgrade notes](https://github.com/stefanpenner/country_select/blob/master/UPGRADING.md) if switching from 1.x.
607
+ * There are a bunch of development dependencies if you plan to contribute to Formtastic
608
+
609
+
610
+ ## How to contribute
611
+
612
+ * Fork the project on Github
613
+ * Install development dependencies (`bundle install` and `bin/appraisal install`)
614
+ * Create a topic branch for your changes
615
+ * Ensure that you provide *documentation* and *test coverage* for your changes (patches won't be accepted without)
616
+ * Ensure that all tests pass (`bundle exec rake`)
617
+ * Create a pull request on Github (these are also a great place to start a conversation around a patch as early as possible)
618
+
619
+
620
+ ## Project Info
621
+
622
+ Formtastic was created by [Justin French](https://www.justinfrench.com) with contributions from around 180 awesome developers. Run `git shortlog -n -s` to see the awesome.
623
+
624
+ The project is hosted on Github: [https://github.com/formtastic/formtastic](https://github.com/formtastic/formtastic), where your contributions, forkings, comments, issues and feedback are greatly welcomed.
625
+
626
+ Copyright (c) 2007-2021, released under the MIT license.
627
+