formtastic 3.1.5 → 4.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitattributes +1 -0
- data/.gitignore +3 -2
- data/.travis.yml +28 -40
- data/CHANGELOG.md +49 -0
- data/DEPRECATIONS +1 -1
- data/Gemfile.lock +104 -0
- data/README.md +628 -629
- data/Rakefile +20 -1
- data/app/assets/stylesheets/formtastic.css +1 -1
- data/bin/appraisal +8 -0
- data/formtastic.gemspec +8 -14
- data/gemfiles/rails_5.2/Gemfile +5 -0
- data/gemfiles/rails_6.0/Gemfile +5 -0
- data/gemfiles/rails_edge/Gemfile +13 -0
- data/lib/formtastic.rb +5 -11
- data/lib/formtastic/actions.rb +6 -3
- data/lib/formtastic/deprecation.rb +1 -38
- data/lib/formtastic/engine.rb +3 -1
- data/lib/formtastic/form_builder.rb +8 -24
- data/lib/formtastic/helpers/action_helper.rb +1 -48
- data/lib/formtastic/helpers/errors_helper.rb +2 -2
- data/lib/formtastic/helpers/fieldset_wrapper.rb +7 -3
- data/lib/formtastic/helpers/input_helper.rb +18 -76
- data/lib/formtastic/helpers/inputs_helper.rb +12 -3
- data/lib/formtastic/i18n.rb +1 -1
- data/lib/formtastic/inputs.rb +32 -29
- data/lib/formtastic/inputs/base/collections.rb +1 -5
- data/lib/formtastic/inputs/base/errors.rb +4 -4
- data/lib/formtastic/inputs/base/hints.rb +1 -1
- data/lib/formtastic/inputs/base/timeish.rb +5 -1
- data/lib/formtastic/inputs/base/validations.rb +19 -9
- data/lib/formtastic/inputs/check_boxes_input.rb +3 -3
- data/lib/formtastic/inputs/color_input.rb +0 -1
- data/lib/formtastic/inputs/select_input.rb +1 -1
- data/lib/formtastic/localizer.rb +5 -7
- data/lib/formtastic/version.rb +1 -1
- data/lib/generators/templates/formtastic.rb +4 -6
- data/script/integration-template.rb +71 -0
- data/script/integration.sh +19 -0
- data/spec/action_class_finder_spec.rb +1 -1
- data/spec/actions/button_action_spec.rb +8 -8
- data/spec/actions/generic_action_spec.rb +60 -60
- data/spec/actions/input_action_spec.rb +7 -7
- data/spec/actions/link_action_spec.rb +10 -10
- data/spec/builder/custom_builder_spec.rb +36 -20
- data/spec/builder/error_proc_spec.rb +4 -4
- data/spec/builder/semantic_fields_for_spec.rb +27 -27
- data/spec/generators/formtastic/form/form_generator_spec.rb +25 -25
- data/spec/generators/formtastic/input/input_generator_spec.rb +31 -31
- data/spec/generators/formtastic/install/install_generator_spec.rb +9 -9
- data/spec/helpers/action_helper_spec.rb +328 -10
- data/spec/helpers/actions_helper_spec.rb +17 -17
- data/spec/helpers/form_helper_spec.rb +33 -33
- data/spec/helpers/input_helper_spec.rb +975 -2
- data/spec/helpers/inputs_helper_spec.rb +120 -105
- data/spec/helpers/reflection_helper_spec.rb +3 -3
- data/spec/helpers/semantic_errors_helper_spec.rb +22 -22
- data/spec/i18n_spec.rb +26 -26
- data/spec/input_class_finder_spec.rb +1 -1
- data/spec/inputs/base/collections_spec.rb +6 -6
- data/spec/inputs/base/validations_spec.rb +157 -19
- data/spec/inputs/boolean_input_spec.rb +55 -55
- data/spec/inputs/check_boxes_input_spec.rb +96 -95
- data/spec/inputs/color_input_spec.rb +51 -63
- data/spec/inputs/country_input_spec.rb +20 -20
- data/spec/inputs/custom_input_spec.rb +2 -6
- data/spec/inputs/datalist_input_spec.rb +1 -1
- data/spec/inputs/date_picker_input_spec.rb +42 -42
- data/spec/inputs/date_select_input_spec.rb +51 -37
- data/spec/inputs/datetime_picker_input_spec.rb +46 -46
- data/spec/inputs/datetime_select_input_spec.rb +53 -37
- data/spec/inputs/email_input_spec.rb +5 -5
- data/spec/inputs/file_input_spec.rb +6 -6
- data/spec/inputs/hidden_input_spec.rb +18 -18
- data/spec/inputs/include_blank_spec.rb +8 -8
- data/spec/inputs/label_spec.rb +20 -20
- data/spec/inputs/number_input_spec.rb +112 -112
- data/spec/inputs/password_input_spec.rb +5 -5
- data/spec/inputs/phone_input_spec.rb +5 -5
- data/spec/inputs/placeholder_spec.rb +5 -5
- data/spec/inputs/radio_input_spec.rb +63 -65
- data/spec/inputs/range_input_spec.rb +66 -66
- data/spec/inputs/readonly_spec.rb +4 -4
- data/spec/inputs/search_input_spec.rb +5 -5
- data/spec/inputs/select_input_spec.rb +92 -96
- data/spec/inputs/string_input_spec.rb +23 -23
- data/spec/inputs/text_input_spec.rb +16 -16
- data/spec/inputs/time_picker_input_spec.rb +43 -43
- data/spec/inputs/time_select_input_spec.rb +67 -54
- data/spec/inputs/time_zone_input_spec.rb +19 -19
- data/spec/inputs/url_input_spec.rb +5 -5
- data/spec/inputs/with_options_spec.rb +7 -7
- data/spec/localizer_spec.rb +17 -17
- data/spec/namespaced_class_finder_spec.rb +2 -2
- data/spec/schema.rb +21 -0
- data/spec/spec_helper.rb +163 -223
- data/spec/support/custom_macros.rb +72 -75
- data/spec/support/shared_examples.rb +0 -1301
- data/spec/support/test_environment.rb +23 -9
- metadata +33 -123
- data/Appraisals +0 -43
- data/CHANGELOG +0 -54
- data/gemfiles/rails_3.2.gemfile +0 -9
- data/gemfiles/rails_4.0.4.gemfile +0 -8
- data/gemfiles/rails_4.1.gemfile +0 -8
- data/gemfiles/rails_4.2.gemfile +0 -8
- data/gemfiles/rails_4.gemfile +0 -8
- data/gemfiles/rails_5.0.gemfile +0 -8
- data/gemfiles/rails_edge.gemfile +0 -15
- data/lib/formtastic/util.rb +0 -57
- data/spec/helpers/namespaced_action_helper_spec.rb +0 -43
- data/spec/helpers/namespaced_input_helper_spec.rb +0 -36
- data/spec/util_spec.rb +0 -66
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8d4e6d380c2d708ed43fd63f5558416c1ef53aa6a208ba9b74ce613419683501
|
4
|
+
data.tar.gz: caf570aee5672261b10094658ab5dcfd56f944f456b288eedebeb1a58d53caf1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e575fe3d770692adfcac597cf9719ab459c157dd09738090eeb9ca5c23abc49ecce78df9a51a3781b55fb5b0f4e50f3f3273af70a8674346a7e059f1485523b8
|
7
|
+
data.tar.gz: 2363a8a9a9ce3b1a981df647d511db68e4fd71073e2a3b4aaa6f0023ae59ae4d402e05a2fc9454d228ca6fd659c3d6e3eceb0e9fbc3df9fb25026006956bef87
|
data/.gitattributes
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
CHANGELOG merge=union
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,46 +1,34 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
cache:
|
2
|
+
- bundler
|
3
|
+
- directories:
|
4
|
+
- gemfiles/rails_52/vendor/bundle
|
5
|
+
- gemfiles/rails_60/vendor/bundle
|
6
|
+
- gemfiles/rails_edge/vendor/bundle
|
7
|
+
dist: bionic
|
3
8
|
language: ruby
|
9
|
+
node_js: 11
|
4
10
|
rvm:
|
5
|
-
-
|
6
|
-
- 2.
|
7
|
-
- 2.
|
8
|
-
- 2.
|
9
|
-
- 2.3.0
|
10
|
-
- 2.4.0
|
11
|
+
- 2.4.9
|
12
|
+
- 2.5.7
|
13
|
+
- 2.6.5
|
14
|
+
- 2.7.0
|
11
15
|
gemfile:
|
12
|
-
- gemfiles/
|
13
|
-
- gemfiles/
|
14
|
-
- gemfiles/
|
15
|
-
|
16
|
-
-
|
17
|
-
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
- gemfiles/rails_5.2/Gemfile
|
17
|
+
- gemfiles/rails_6.0/Gemfile
|
18
|
+
- gemfiles/rails_edge/Gemfile
|
19
|
+
before_install:
|
20
|
+
- gem install bundler
|
21
|
+
- export BUNDLE_PATH=$(dirname $(readlink -f $BUNDLE_GEMFILE))/vendor/bundle
|
22
|
+
before_cache:
|
23
|
+
- rm -f ${BUNDLE_PATH}/**/extensions/**/{gem_make.out,mkmf.log}
|
24
|
+
script:
|
25
|
+
- bundle exec rake spec
|
26
|
+
- script/integration.sh
|
22
27
|
matrix:
|
23
28
|
exclude:
|
24
|
-
- rvm:
|
25
|
-
gemfile: gemfiles/
|
26
|
-
- rvm: 2.
|
27
|
-
gemfile: gemfiles/rails_edge
|
28
|
-
- rvm: 2.1.8
|
29
|
-
gemfile: gemfiles/rails_edge.gemfile
|
30
|
-
- rvm: 2.0.0
|
31
|
-
gemfile: gemfiles/rails_5.0.gemfile
|
32
|
-
- rvm: 2.1.8
|
33
|
-
gemfile: gemfiles/rails_5.0.gemfile
|
34
|
-
- rvm: 2.4.0
|
35
|
-
gemfile: gemfiles/rails_4.1.gemfile
|
36
|
-
- rvm: 2.4.0
|
37
|
-
gemfile: gemfiles/rails_4.2.gemfile
|
38
|
-
- rvm: 2.4.0
|
39
|
-
gemfile: gemfiles/rails_3.2.gemfile
|
40
|
-
- rvm: 2.4.0
|
41
|
-
gemfile: gemfiles/rails_4.0.4.gemfile
|
42
|
-
- rvm: 2.4.0
|
43
|
-
gemfile: gemfiles/rails_4.gemfile
|
29
|
+
- rvm: 2.4.9
|
30
|
+
gemfile: gemfiles/rails_6.0/Gemfile
|
31
|
+
- rvm: 2.4.9
|
32
|
+
gemfile: gemfiles/rails_edge/Gemfile
|
44
33
|
allow_failures:
|
45
|
-
- gemfile: gemfiles/rails_edge
|
46
|
-
- rvm: 1.9.3
|
34
|
+
- gemfile: gemfiles/rails_edge/Gemfile
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
## master
|
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
|
+
|
15
|
+
## 3.1.2
|
16
|
+
|
17
|
+
* Fixed that we specified 4.0.4 instead of 4.1 in the Rails version deprecation message
|
18
|
+
|
19
|
+
## 3.1.1
|
20
|
+
|
21
|
+
* Fixed class custom input & action class loading in test environments
|
22
|
+
* Added documentation of custom input & action class finders
|
23
|
+
* Added a link to documentation & wiki from custom class deprecation warnings
|
24
|
+
|
25
|
+
## 3.1.0
|
26
|
+
|
27
|
+
* Performance and documentation improvements
|
28
|
+
|
29
|
+
## 3.1.0.rc2
|
30
|
+
|
31
|
+
* Deprecated :member_value and :member_label options
|
32
|
+
|
33
|
+
## 3.1.0.rc1
|
34
|
+
|
35
|
+
* Deprecated support for Rails version < 4.1.0
|
36
|
+
* Fixed synchronization issues with custom_namespace configuration
|
37
|
+
* Fixed bug where boolean (checkbox) inputs were not being correctly checked (also in 2.3.1)
|
38
|
+
* Fixed (silenced) Rails 5 deprecation on column_for_attribute that we're handling fine
|
39
|
+
* Added new DatalistInput (:as => :datalist) for HTML5 datalists
|
40
|
+
* Added configurable namespaces for custom inputs
|
41
|
+
* Various performance and documentation improvements
|
42
|
+
|
43
|
+
---
|
44
|
+
|
45
|
+
See 3.0-stable branch for 3.0.x changes
|
46
|
+
https://github.com/justinfrench/formtastic/blob/3.0-stable/CHANGELOG
|
47
|
+
|
48
|
+
See 2.3-stable branch for 2.3.x and earlier releases
|
49
|
+
https://github.com/justinfrench/formtastic/blob/2.3-stable/CHANGELOG
|
data/DEPRECATIONS
CHANGED
@@ -3,7 +3,7 @@ Formtastic features and compatibilities.
|
|
3
3
|
|
4
4
|
v4.0 (planned)
|
5
5
|
|
6
|
-
* Remove support for Rails <
|
6
|
+
* Remove support for Rails < 5.2
|
7
7
|
* Remove support for country_select 1.x syntax (they want to remove it in 3.0)
|
8
8
|
* Remove support for input_class, custom_input_class_name, standard_input_class_name, action_class, custom_action_class_name, standard_action_class_name
|
9
9
|
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
formtastic (3.2.0.pre)
|
5
|
+
actionpack (>= 5.2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionpack (6.0.3.2)
|
11
|
+
actionview (= 6.0.3.2)
|
12
|
+
activesupport (= 6.0.3.2)
|
13
|
+
rack (~> 2.0, >= 2.0.8)
|
14
|
+
rack-test (>= 0.6.3)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
17
|
+
actionview (6.0.3.2)
|
18
|
+
activesupport (= 6.0.3.2)
|
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.0.3.2)
|
24
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
25
|
+
i18n (>= 0.7, < 2)
|
26
|
+
minitest (~> 5.1)
|
27
|
+
tzinfo (~> 1.1)
|
28
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
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.6)
|
35
|
+
crass (1.0.6)
|
36
|
+
diff-lcs (1.3)
|
37
|
+
erubi (1.9.0)
|
38
|
+
i18n (1.8.3)
|
39
|
+
concurrent-ruby (~> 1.0)
|
40
|
+
loofah (2.6.0)
|
41
|
+
crass (~> 1.0.2)
|
42
|
+
nokogiri (>= 1.5.9)
|
43
|
+
method_source (1.0.0)
|
44
|
+
mini_portile2 (2.4.0)
|
45
|
+
minitest (5.14.1)
|
46
|
+
nokogiri (1.10.9)
|
47
|
+
mini_portile2 (~> 2.4.0)
|
48
|
+
rack (2.2.3)
|
49
|
+
rack-test (1.1.0)
|
50
|
+
rack (>= 1.0, < 3)
|
51
|
+
rails-dom-testing (2.0.3)
|
52
|
+
activesupport (>= 4.2.0)
|
53
|
+
nokogiri (>= 1.6)
|
54
|
+
rails-html-sanitizer (1.3.0)
|
55
|
+
loofah (~> 2.3)
|
56
|
+
railties (6.0.3.2)
|
57
|
+
actionpack (= 6.0.3.2)
|
58
|
+
activesupport (= 6.0.3.2)
|
59
|
+
method_source
|
60
|
+
rake (>= 0.8.7)
|
61
|
+
thor (>= 0.20.3, < 2.0)
|
62
|
+
rake (13.0.1)
|
63
|
+
rspec-core (3.9.2)
|
64
|
+
rspec-support (~> 3.9.3)
|
65
|
+
rspec-dom-testing (0.1.0)
|
66
|
+
rails-dom-testing (>= 1.0, < 3)
|
67
|
+
rspec-expectations (~> 3.0)
|
68
|
+
rspec-expectations (3.9.2)
|
69
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
70
|
+
rspec-support (~> 3.9.0)
|
71
|
+
rspec-mocks (3.9.1)
|
72
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
73
|
+
rspec-support (~> 3.9.0)
|
74
|
+
rspec-rails (3.9.1)
|
75
|
+
actionpack (>= 3.0)
|
76
|
+
activesupport (>= 3.0)
|
77
|
+
railties (>= 3.0)
|
78
|
+
rspec-core (~> 3.9.0)
|
79
|
+
rspec-expectations (~> 3.9.0)
|
80
|
+
rspec-mocks (~> 3.9.0)
|
81
|
+
rspec-support (~> 3.9.0)
|
82
|
+
rspec-support (3.9.3)
|
83
|
+
sqlite3 (1.4.2)
|
84
|
+
thor (1.0.1)
|
85
|
+
thread_safe (0.3.6)
|
86
|
+
tzinfo (1.2.7)
|
87
|
+
thread_safe (~> 0.1)
|
88
|
+
yard (0.9.25)
|
89
|
+
zeitwerk (2.3.0)
|
90
|
+
|
91
|
+
PLATFORMS
|
92
|
+
ruby
|
93
|
+
|
94
|
+
DEPENDENCIES
|
95
|
+
ammeter (~> 1.1.3)
|
96
|
+
formtastic!
|
97
|
+
rake
|
98
|
+
rspec-dom-testing (>= 0.1.0)
|
99
|
+
rspec-rails (~> 3.4)
|
100
|
+
sqlite3 (~> 1.4)
|
101
|
+
yard (~> 0.9.20)
|
102
|
+
|
103
|
+
BUNDLED WITH
|
104
|
+
2.1.4
|
data/README.md
CHANGED
@@ -1,629 +1,628 @@
|
|
1
|
-
# Formtastic
|
2
|
-
|
3
|
-
[![Build Status](https://travis-ci.org/justinfrench/formtastic.svg?branch=master)](https://travis-ci.org/justinfrench/formtastic)
|
4
|
-
[![Inline docs](
|
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
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
* [
|
14
|
-
* [We
|
15
|
-
* [
|
16
|
-
* [
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
* Formtastic
|
22
|
-
* Formtastic
|
23
|
-
* Formtastic
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
<%= f.input :
|
35
|
-
<%= f.input :
|
36
|
-
<%= f.input :
|
37
|
-
<%= f.input :
|
38
|
-
<%= f.input :
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
<%= f.input :
|
44
|
-
<%= f.input :
|
45
|
-
<%= f.input :
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
<%= author_form.input :
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
<%= f.action :
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
* It can handle `
|
68
|
-
* It
|
69
|
-
* It
|
70
|
-
* It
|
71
|
-
*
|
72
|
-
*
|
73
|
-
* It
|
74
|
-
* It
|
75
|
-
*
|
76
|
-
*
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
*
|
83
|
-
*
|
84
|
-
*
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
*= require
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
<!--[if IE
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
<%= f.
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
<%= f.
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
<%= f.input :
|
167
|
-
<%= f.input :
|
168
|
-
<%= f.input :
|
169
|
-
<%= f.input :
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
<%= f.action :
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
<%= f.input :
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
<%= f.input :
|
189
|
-
<%= f.input :
|
190
|
-
<%= f.input :
|
191
|
-
<%= f.input :
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
<%= f.
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
<%= f.inputs :
|
223
|
-
<%= f.
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
<%= f.inputs %>
|
233
|
-
<%= f.
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
<%= f.input :
|
254
|
-
<%= f.input :
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
<%= f.input :
|
267
|
-
<%= f.input :
|
268
|
-
<%= f.input :
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
<%= f.input :
|
292
|
-
<%= f.input :
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
f.input :authors, :as => :check_boxes, :collection =>
|
303
|
-
f.input :authors, :as => :check_boxes, :collection =>
|
304
|
-
f.input :authors, :as => :check_boxes, :collection => [
|
305
|
-
f.input :
|
306
|
-
f.input :author, :as => :select, :collection => Author.
|
307
|
-
f.input :author, :as => :select, :collection => Author.pluck(
|
308
|
-
f.input :author, :as => :select, :collection => Author.
|
309
|
-
f.input :author, :as => :select, :collection =>
|
310
|
-
f.input :author, :as => :select, :collection =>
|
311
|
-
f.input :author, :as => :
|
312
|
-
f.input :author, :as => :radio, :collection =>
|
313
|
-
f.input :author, :as => :radio, :collection =>
|
314
|
-
f.input :author, :as => :radio, :collection =>
|
315
|
-
f.input :
|
316
|
-
f.input :
|
317
|
-
f.input :
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
* `:
|
327
|
-
* `:
|
328
|
-
* `:
|
329
|
-
* `:
|
330
|
-
* `:
|
331
|
-
* `:
|
332
|
-
* `:
|
333
|
-
* `:
|
334
|
-
* `:
|
335
|
-
* `:
|
336
|
-
* `:
|
337
|
-
* `:
|
338
|
-
* `:
|
339
|
-
* `:
|
340
|
-
* `:
|
341
|
-
* `:
|
342
|
-
* `:
|
343
|
-
* `:
|
344
|
-
* `:
|
345
|
-
* `:
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
<%= f.input :
|
374
|
-
<%= f.input :
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
<%= f.input :
|
429
|
-
<%= f.input :
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
<%= f.input :
|
457
|
-
<%= f.input :
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
<%= f.input :
|
478
|
-
<%= f.input :
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
* `
|
492
|
-
* `
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
*
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
*
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
*
|
615
|
-
*
|
616
|
-
*
|
617
|
-
* Ensure that
|
618
|
-
*
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
1
|
+
# Formtastic
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/justinfrench/formtastic.svg?branch=master)](https://travis-ci.org/justinfrench/formtastic)
|
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/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
|
+
|
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/justinfrench/formtastic)
|
13
|
+
* [We track issues & bugs on GitHub](https://github.com/justinfrench/formtastic/issues)
|
14
|
+
* [We have a wiki on GitHub](https://github.com/justinfrench/formtastic/wiki)
|
15
|
+
* [StackOverflow can help](https://stackoverflow.com/questions/tagged/formtastic)
|
16
|
+
* [Follow @formtastic on Twitter for news & updates](https://twitter.com/formtastic)
|
17
|
+
|
18
|
+
## Compatibility
|
19
|
+
|
20
|
+
* Formtastic 4 will require Rails 5.2 and Ruby 2.4 minimum
|
21
|
+
* Formtastic 3 requires Rails 3.2.13 minimum
|
22
|
+
* Formtastic 2 requires Rails 3
|
23
|
+
* 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!
|
24
|
+
|
25
|
+
## The Story
|
26
|
+
|
27
|
+
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:
|
28
|
+
|
29
|
+
```erb
|
30
|
+
<%= semantic_form_for @article do |f| %>
|
31
|
+
|
32
|
+
<%= f.inputs :name => "Basic" do %>
|
33
|
+
<%= f.input :title %>
|
34
|
+
<%= f.input :body %>
|
35
|
+
<%= f.input :section %>
|
36
|
+
<%= f.input :publication_state, :as => :radio %>
|
37
|
+
<%= f.input :category %>
|
38
|
+
<%= f.input :allow_comments, :label => "Allow commenting on this article" %>
|
39
|
+
<% end %>
|
40
|
+
|
41
|
+
<%= f.inputs :name => "Advanced" do %>
|
42
|
+
<%= f.input :keywords, :required => false, :hint => "Example: ruby, rails, forms" %>
|
43
|
+
<%= f.input :extract, :required => false %>
|
44
|
+
<%= f.input :description, :required => false %>
|
45
|
+
<%= f.input :url_title, :required => false %>
|
46
|
+
<% end %>
|
47
|
+
|
48
|
+
<%= f.inputs :name => "Author", :for => :author do |author_form| %>
|
49
|
+
<%= author_form.input :first_name %>
|
50
|
+
<%= author_form.input :last_name %>
|
51
|
+
<% end %>
|
52
|
+
|
53
|
+
<%= f.actions do %>
|
54
|
+
<%= f.action :submit, :as => :button %>
|
55
|
+
<%= f.action :cancel, :as => :link %>
|
56
|
+
<% end %>
|
57
|
+
|
58
|
+
<% end %>
|
59
|
+
```
|
60
|
+
|
61
|
+
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.
|
62
|
+
|
63
|
+
|
64
|
+
## It's awesome because...
|
65
|
+
|
66
|
+
* 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.
|
67
|
+
* 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.
|
68
|
+
* It's Rails 3/4 compatible (including nested forms).
|
69
|
+
* It has internationalization (I18n)!
|
70
|
+
* 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.
|
71
|
+
* There's heaps of elements, id and class attributes for you to hook in your CSS and JS.
|
72
|
+
* It handles real world stuff like inline hints, inline error messages & help text.
|
73
|
+
* 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).
|
74
|
+
* It's got absolutely awesome spec coverage.
|
75
|
+
* There's a bunch of people using and working on it (it's not just one developer building half a solution).
|
76
|
+
* It has growing HTML5 support (new inputs like email/phone/search, new attributes like required/min/max/step/placeholder)
|
77
|
+
|
78
|
+
|
79
|
+
## Opinions
|
80
|
+
|
81
|
+
* It should be easier to do things the right way than the wrong way.
|
82
|
+
* Sometimes _more mark-up_ is better.
|
83
|
+
* Elements and attribute hooks are _gold_ for stylesheet authors.
|
84
|
+
* Make the common things we do easy, yet ensure uncommon things are still possible.
|
85
|
+
|
86
|
+
|
87
|
+
## Installation
|
88
|
+
|
89
|
+
Simply add Formtastic to your Gemfile and bundle it up:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
gem 'formtastic', '~> 3.0'
|
93
|
+
```
|
94
|
+
|
95
|
+
Run the installation generator:
|
96
|
+
|
97
|
+
```shell
|
98
|
+
$ rails generate formtastic:install
|
99
|
+
```
|
100
|
+
|
101
|
+
|
102
|
+
## Stylesheets
|
103
|
+
|
104
|
+
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.
|
105
|
+
|
106
|
+
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.
|
107
|
+
|
108
|
+
```css
|
109
|
+
# app/assets/stylesheets/application.css
|
110
|
+
*= require formtastic
|
111
|
+
*= require my_formtastic_changes
|
112
|
+
```
|
113
|
+
|
114
|
+
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.
|
115
|
+
|
116
|
+
```css
|
117
|
+
# app/assets/stylesheets/ie6.css
|
118
|
+
*= require formtastic_ie6
|
119
|
+
|
120
|
+
# app/assets/stylesheets/ie7.css
|
121
|
+
*= require formtastic_ie7
|
122
|
+
```
|
123
|
+
|
124
|
+
```erb
|
125
|
+
# app/views/layouts/application.html.erb
|
126
|
+
<%= stylesheet_link_tag 'application' %>
|
127
|
+
<!--[if IE 6]><%= stylesheet_link_tag 'ie6' %><![endif]-->
|
128
|
+
<!--[if IE 7]><%= stylesheet_link_tag 'ie7' %><![endif]-->
|
129
|
+
```
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
# config/environments/production.rb
|
133
|
+
config.assets.precompile += %w( ie6.css ie7.css )
|
134
|
+
```
|
135
|
+
|
136
|
+
## Usage
|
137
|
+
|
138
|
+
Forms are really boring to code... you want to get onto the good stuff as fast as possible.
|
139
|
+
|
140
|
+
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):
|
141
|
+
|
142
|
+
```erb
|
143
|
+
<%= semantic_form_for @user do |f| %>
|
144
|
+
<%= f.inputs %>
|
145
|
+
<%= f.actions %>
|
146
|
+
<% end %>
|
147
|
+
```
|
148
|
+
|
149
|
+
This is a great way to get something up fast, but like scaffolding, it's *not recommended for production*. Don't be so lazy!
|
150
|
+
|
151
|
+
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`:
|
152
|
+
|
153
|
+
```erb
|
154
|
+
<%= semantic_form_for @user do |f| %>
|
155
|
+
<%= f.inputs :title, :body, :section, :categories, :created_at %>
|
156
|
+
<%= f.actions :submit, :cancel %>
|
157
|
+
<% end %>
|
158
|
+
```
|
159
|
+
|
160
|
+
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:
|
161
|
+
|
162
|
+
```erb
|
163
|
+
<%= semantic_form_for @post do |f| %>
|
164
|
+
<%= f.inputs do %>
|
165
|
+
<%= f.input :title %>
|
166
|
+
<%= f.input :body %>
|
167
|
+
<%= f.input :section, :as => :radio %>
|
168
|
+
<%= f.input :categories %>
|
169
|
+
<%= f.input :created_at, :as => :string %>
|
170
|
+
<% end %>
|
171
|
+
<%= f.actions do %>
|
172
|
+
<%= f.action :submit, :as => :button %>
|
173
|
+
<%= f.action :cancel, :as => :link %>
|
174
|
+
<% end %>
|
175
|
+
<% end %>
|
176
|
+
```
|
177
|
+
|
178
|
+
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:
|
179
|
+
|
180
|
+
```erb
|
181
|
+
<%= semantic_form_for @post do |f| %>
|
182
|
+
<%= f.inputs "Basic", :id => "basic" do %>
|
183
|
+
<%= f.input :title %>
|
184
|
+
<%= f.input :body %>
|
185
|
+
<% end %>
|
186
|
+
<%= f.inputs :name => "Advanced Options", :id => "advanced" do %>
|
187
|
+
<%= f.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
|
188
|
+
<%= f.input :section, :as => :radio %>
|
189
|
+
<%= f.input :user, :label => "Author" %>
|
190
|
+
<%= f.input :categories, :required => false %>
|
191
|
+
<%= f.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
|
192
|
+
<% end %>
|
193
|
+
<%= f.actions do %>
|
194
|
+
<%= f.action :submit %>
|
195
|
+
<% end %>
|
196
|
+
<% end %>
|
197
|
+
```
|
198
|
+
|
199
|
+
You can create forms for nested resources:
|
200
|
+
|
201
|
+
```erb
|
202
|
+
<%= semantic_form_for [@author, @post] do |f| %>
|
203
|
+
```
|
204
|
+
|
205
|
+
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:
|
206
|
+
|
207
|
+
```erb
|
208
|
+
<%= semantic_form_for @post do |f| %>
|
209
|
+
<%= f.inputs :title, :body, :created_at %>
|
210
|
+
<%= f.semantic_fields_for :author do |author| %>
|
211
|
+
<%= author.inputs :first_name, :last_name, :name => "Author" %>
|
212
|
+
<% end %>
|
213
|
+
<%= f.actions %>
|
214
|
+
<% end %>
|
215
|
+
```
|
216
|
+
|
217
|
+
Or the Formtastic way with the `:for` option:
|
218
|
+
|
219
|
+
```erb
|
220
|
+
<%= semantic_form_for @post do |f| %>
|
221
|
+
<%= f.inputs :title, :body, :created_at %>
|
222
|
+
<%= f.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
|
223
|
+
<%= f.actions %>
|
224
|
+
<% end %>
|
225
|
+
```
|
226
|
+
|
227
|
+
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:
|
228
|
+
|
229
|
+
```erb
|
230
|
+
<%= semantic_form_for @post do |f| %>
|
231
|
+
<%= f.inputs %>
|
232
|
+
<%= f.inputs :name => 'Category #%i', :for => :categories %>
|
233
|
+
<%= f.actions %>
|
234
|
+
<% end %>
|
235
|
+
```
|
236
|
+
|
237
|
+
Alternatively, the current index can be accessed via the `inputs` block's arguments for use anywhere:
|
238
|
+
|
239
|
+
```erb
|
240
|
+
<%= semantic_form_for @post do |f| %>
|
241
|
+
<%= f.inputs :for => :categories do |category, i| %>
|
242
|
+
...
|
243
|
+
<%= f.actions %>
|
244
|
+
<% end %>
|
245
|
+
```
|
246
|
+
|
247
|
+
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:
|
248
|
+
|
249
|
+
```erb
|
250
|
+
<%= semantic_form_for(@post, :namespace => 'cat_form') do |f| %>
|
251
|
+
<%= f.inputs do %>
|
252
|
+
<%= f.input :title %> # id="cat_form_post_title"
|
253
|
+
<%= f.input :body %> # id="cat_form_post_body"
|
254
|
+
<%= f.input :created_at %> # id="cat_form_post_created_at"
|
255
|
+
<% end %>
|
256
|
+
<%= f.actions %>
|
257
|
+
<% end %>
|
258
|
+
```
|
259
|
+
|
260
|
+
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:
|
261
|
+
|
262
|
+
```erb
|
263
|
+
<%= semantic_form_for @post do |f| %>
|
264
|
+
<%= f.inputs do %>
|
265
|
+
<%= f.input :title, :input_html => { :size => 10 } %>
|
266
|
+
<%= f.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 } %>
|
267
|
+
<%= f.input :created_at, :input_html => { :disabled => true } %>
|
268
|
+
<%= f.input :updated_at, :input_html => { :readonly => true } %>
|
269
|
+
<% end %>
|
270
|
+
<%= f.actions %>
|
271
|
+
<% end %>
|
272
|
+
```
|
273
|
+
|
274
|
+
The same can be done for actions with the `:button_html` option:
|
275
|
+
|
276
|
+
```erb
|
277
|
+
<%= semantic_form_for @post do |f| %>
|
278
|
+
...
|
279
|
+
<%= f.actions do %>
|
280
|
+
<%= f.action :submit, :button_html => { :class => "primary", :disable_with => 'Wait...' } %>
|
281
|
+
<% end %>
|
282
|
+
<% end %>
|
283
|
+
```
|
284
|
+
|
285
|
+
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"`).
|
286
|
+
|
287
|
+
```erb
|
288
|
+
<%= semantic_form_for @post do |f| %>
|
289
|
+
<%= f.inputs do %>
|
290
|
+
<%= f.input :title, :wrapper_html => { :class => "important" } %>
|
291
|
+
<%= f.input :body %>
|
292
|
+
<%= f.input :description, :wrapper_html => { :style => "display:none;" } %>
|
293
|
+
<% end %>
|
294
|
+
...
|
295
|
+
<% end %>
|
296
|
+
```
|
297
|
+
|
298
|
+
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:
|
299
|
+
|
300
|
+
```ruby
|
301
|
+
f.input :authors, :as => :check_boxes, :collection => User.order("last_name ASC").all
|
302
|
+
f.input :authors, :as => :check_boxes, :collection => current_user.company.users.active
|
303
|
+
f.input :authors, :as => :check_boxes, :collection => [@justin, @kate]
|
304
|
+
f.input :authors, :as => :check_boxes, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
|
305
|
+
f.input :author, :as => :select, :collection => Author.all
|
306
|
+
f.input :author, :as => :select, :collection => Author.pluck(:first_name, :id)
|
307
|
+
f.input :author, :as => :select, :collection => Author.pluck(Arel.sql("CONCAT(`first_name`, ' ', `last_name`)"), :id)
|
308
|
+
f.input :author, :as => :select, :collection => Author.your_custom_scope_or_class_method
|
309
|
+
f.input :author, :as => :select, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
|
310
|
+
f.input :author, :as => :select, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
|
311
|
+
f.input :author, :as => :radio, :collection => User.all
|
312
|
+
f.input :author, :as => :radio, :collection => [@justin, @kate]
|
313
|
+
f.input :author, :as => :radio, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
|
314
|
+
f.input :author, :as => :radio, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
|
315
|
+
f.input :admin, :as => :radio, :collection => ["Yes!", "No"]
|
316
|
+
f.input :book_id, :as => :select, :collection => Hash[Book.all.map{|b| [b.name,b.id]}]
|
317
|
+
f.input :fav_book,:as => :datalist , :collection => Book.pluck(:name)
|
318
|
+
```
|
319
|
+
|
320
|
+
|
321
|
+
## The Available Inputs
|
322
|
+
|
323
|
+
The Formtastic input types:
|
324
|
+
|
325
|
+
* `:select` - a select menu. Default for ActiveRecord associations: `belongs_to`, `has_many`, and `has_and_belongs_to_many`.
|
326
|
+
* `:check_boxes` - a set of check_box inputs. Alternative to `:select` for ActiveRecord-associations: `has_many`, and has_and_belongs_to_many`.
|
327
|
+
* `:radio` - a set of radio inputs. Alternative to `:select` for ActiveRecord-associations: `belongs_to`.
|
328
|
+
* `:time_zone` - a select input. Default for column types: `:string` with name matching `"time_zone"`.
|
329
|
+
* `:password` - a password input. Default for column types: `:string` with name matching `"password"`.
|
330
|
+
* `:text` - a textarea. Default for column types: `:text`.
|
331
|
+
* `:date_select` - a date select. Default for column types: `:date`.
|
332
|
+
* `:datetime_select` - a date and time select. Default for column types: `:datetime` and `:timestamp`.
|
333
|
+
* `:time_select` - a time select. Default for column types: `:time`.
|
334
|
+
* `:boolean` - a checkbox. Default for column types: `:boolean`.
|
335
|
+
* `:string` - a text field. Default for column types: `:string`.
|
336
|
+
* `:number` - a text field (just like string). Default for column types: `:integer`, `:float`, and `:decimal`.
|
337
|
+
* `: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).
|
338
|
+
* `:country` - a select menu of country names. Default for column types: `:string` with name `"country"` - requires a *country_select* plugin to be installed.
|
339
|
+
* `:email` - a text field (just like string). Default for columns with name matching `"email"`. New in HTML5. Works on some mobile browsers already.
|
340
|
+
* `:url` - a text field (just like string). Default for columns with name matching `"url"`. New in HTML5. Works on some mobile browsers already.
|
341
|
+
* `:phone` - a text field (just like string). Default for columns with name matching `"phone"` or `"fax"`. New in HTML5.
|
342
|
+
* `:search` - a text field (just like string). Default for columns with name matching `"search"`. New in HTML5. Works on Safari.
|
343
|
+
* `:hidden` - a hidden field. Creates a hidden field (added for compatibility).
|
344
|
+
* `:range` - a slider field.
|
345
|
+
* `:datalist` - a text field with a accompanying [datalist tag](https://developer.mozilla.org/en/docs/Web/HTML/Element/datalist) which provides options for autocompletion
|
346
|
+
|
347
|
+
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.
|
348
|
+
|
349
|
+
|
350
|
+
## Delegation for label lookups
|
351
|
+
|
352
|
+
Formtastic decides which label to use in the following order:
|
353
|
+
|
354
|
+
```
|
355
|
+
1. :label # :label => "Choose Title"
|
356
|
+
2. Formtastic i18n # if either :label => true || i18n_lookups_by_default = true (see Internationalization)
|
357
|
+
3. Activerecord i18n # if localization file found for the given attribute
|
358
|
+
4. label_str_method # if nothing provided this defaults to :humanize but can be set to a custom method
|
359
|
+
```
|
360
|
+
|
361
|
+
## Internationalization (I18n)
|
362
|
+
|
363
|
+
### Basic Localization
|
364
|
+
|
365
|
+
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.
|
366
|
+
|
367
|
+
Basic localization (labels only, with ActiveRecord):
|
368
|
+
|
369
|
+
```erb
|
370
|
+
<%= semantic_form_for @post do |f| %>
|
371
|
+
<%= f.inputs do %>
|
372
|
+
<%= f.input :title %> # => :label => I18n.t('activerecord.attributes.user.title') or 'Title'
|
373
|
+
<%= f.input :body %> # => :label => I18n.t('activerecord.attributes.user.body') or 'Body'
|
374
|
+
<%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
|
375
|
+
<% end %>
|
376
|
+
<% end %>
|
377
|
+
```
|
378
|
+
|
379
|
+
*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?
|
380
|
+
|
381
|
+
### Enhanced Localization (Formtastic I18n API)
|
382
|
+
|
383
|
+
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:
|
384
|
+
|
385
|
+
*1. Enable I18n lookups by default (`config/initializers/formtastic.rb`):*
|
386
|
+
|
387
|
+
```ruby
|
388
|
+
Formtastic::FormBuilder.i18n_lookups_by_default = true
|
389
|
+
```
|
390
|
+
|
391
|
+
*2. Add some label-translations/variants (`config/locales/en.yml`):*
|
392
|
+
|
393
|
+
```yml
|
394
|
+
en:
|
395
|
+
formtastic:
|
396
|
+
titles:
|
397
|
+
post_details: "Post details"
|
398
|
+
labels:
|
399
|
+
post:
|
400
|
+
title: "Your Title"
|
401
|
+
body: "Write something..."
|
402
|
+
edit:
|
403
|
+
title: "Edit title"
|
404
|
+
hints:
|
405
|
+
post:
|
406
|
+
title: "Choose a good title for your post."
|
407
|
+
body: "Write something inspiring here."
|
408
|
+
placeholders:
|
409
|
+
post:
|
410
|
+
title: "Title your post"
|
411
|
+
slug: "Leave blank for an automatically generated slug"
|
412
|
+
user:
|
413
|
+
email: "you@yours.com"
|
414
|
+
actions:
|
415
|
+
create: "Create my %{model}"
|
416
|
+
update: "Save changes"
|
417
|
+
reset: "Reset form"
|
418
|
+
cancel: "Cancel and go back"
|
419
|
+
dummie: "Launch!"
|
420
|
+
```
|
421
|
+
|
422
|
+
*3. ...and now you'll get:*
|
423
|
+
|
424
|
+
```erb
|
425
|
+
<%= semantic_form_for Post.new do |f| %>
|
426
|
+
<%= f.inputs do %>
|
427
|
+
<%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
|
428
|
+
<%= f.input :body %> # => :label => "Write something...", :hint => "Write something inspiring here."
|
429
|
+
<%= f.input :section %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
|
430
|
+
<% end %>
|
431
|
+
<%= f.actions do %>
|
432
|
+
<%= f.action :submit %> # => "Create my %{model}"
|
433
|
+
<% end %>
|
434
|
+
<% end %>
|
435
|
+
```
|
436
|
+
|
437
|
+
*4. Localized titles (a.k.a. legends):*
|
438
|
+
|
439
|
+
_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._
|
440
|
+
|
441
|
+
```erb
|
442
|
+
<%= semantic_form_for @post do |f| %>
|
443
|
+
<%= f.inputs :post_details do %> # => :title => "Post details"
|
444
|
+
# ...
|
445
|
+
<% end %>
|
446
|
+
# ...
|
447
|
+
<% end %>
|
448
|
+
```
|
449
|
+
|
450
|
+
*5. Override I18n settings:*
|
451
|
+
|
452
|
+
```erb
|
453
|
+
<%= semantic_form_for @post do |f| %>
|
454
|
+
<%= f.inputs do %>
|
455
|
+
<%= f.input :title %> # => :label => "Choose a title...", :hint => "Choose a good title for your post."
|
456
|
+
<%= f.input :body, :hint => false %> # => :label => "Write something..."
|
457
|
+
<%= f.input :section, :label => 'Some section' %> # => :label => 'Some section'
|
458
|
+
<% end %>
|
459
|
+
<%= f.actions do %>
|
460
|
+
<%= f.action :submit, :label => :dummie %> # => "Launch!"
|
461
|
+
<% end %>
|
462
|
+
<% end %>
|
463
|
+
```
|
464
|
+
|
465
|
+
If I18n-lookups is disabled, i.e.:
|
466
|
+
|
467
|
+
```ruby
|
468
|
+
Formtastic::FormBuilder.i18n_lookups_by_default = false
|
469
|
+
```
|
470
|
+
|
471
|
+
...then you can enable I18n within the forms instead:
|
472
|
+
|
473
|
+
```erb
|
474
|
+
<%= semantic_form_for @post do |f| %>
|
475
|
+
<%= f.inputs do %>
|
476
|
+
<%= f.input :title, :label => true %> # => :label => "Choose a title..."
|
477
|
+
<%= f.input :body, :label => true %> # => :label => "Write something..."
|
478
|
+
<%= f.input :section, :label => true %> # => :label => I18n.t('activerecord.attributes.user.section') or 'Section'
|
479
|
+
<% end %>
|
480
|
+
<%= f.actions do %>
|
481
|
+
<%= f.action :submit, :label => true %> # => "Update %{model}" (if we are in edit that is...)
|
482
|
+
<% end %>
|
483
|
+
<% end %>
|
484
|
+
```
|
485
|
+
|
486
|
+
*6. Advanced I18n lookups*
|
487
|
+
|
488
|
+
For more flexible forms; Formtastic finds translations using a bottom-up approach taking the following variables in account:
|
489
|
+
|
490
|
+
* `MODEL`, e.g. "post"
|
491
|
+
* `ACTION`, e.g. "edit"
|
492
|
+
* `KEY/ATTRIBUTE`, e.g. "title", :my_custom_key, ...
|
493
|
+
|
494
|
+
...in the following order:
|
495
|
+
|
496
|
+
1. `formtastic.{titles,labels,hints,actions}.MODEL.ACTION.ATTRIBUTE` - by model and action
|
497
|
+
2. `formtastic.{titles,labels,hints,actions}.MODEL.ATTRIBUTE` - by model
|
498
|
+
3. `formtastic.{titles,labels,hints,actions}.ATTRIBUTE` - global default
|
499
|
+
|
500
|
+
...which means that you can define translations like this:
|
501
|
+
|
502
|
+
```yml
|
503
|
+
en:
|
504
|
+
formtastic:
|
505
|
+
labels:
|
506
|
+
title: "Title" # Default global value
|
507
|
+
article:
|
508
|
+
body: "Article content"
|
509
|
+
post:
|
510
|
+
new:
|
511
|
+
title: "Choose a title..."
|
512
|
+
body: "Write something..."
|
513
|
+
edit:
|
514
|
+
title: "Edit title"
|
515
|
+
body: "Edit body"
|
516
|
+
```
|
517
|
+
|
518
|
+
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.
|
519
|
+
|
520
|
+
*7. Basic Translations*
|
521
|
+
If you want some basic translations, take a look on the [formtastic_i18n gem](https://github.com/timoschilling/formtastic_i18n).
|
522
|
+
|
523
|
+
## Semantic errors
|
524
|
+
|
525
|
+
You can show errors on base (by default) and any other attribute just by passing its name to the semantic_errors method:
|
526
|
+
|
527
|
+
```erb
|
528
|
+
<%= semantic_form_for @post do |f| %>
|
529
|
+
<%= f.semantic_errors :state %>
|
530
|
+
<% end %>
|
531
|
+
```
|
532
|
+
|
533
|
+
|
534
|
+
## Modified & Custom Inputs
|
535
|
+
|
536
|
+
You can modify existing inputs, subclass them, or create your own from scratch. Here's the basic process:
|
537
|
+
|
538
|
+
* 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.
|
539
|
+
* To use that input, leave off the word "input" in your `as` statement. For example, `f.input(:size, :as => :hat_size)`
|
540
|
+
|
541
|
+
Specific examples follow.
|
542
|
+
|
543
|
+
### Changing Existing Input Behavior
|
544
|
+
|
545
|
+
To modify the behavior of `StringInput`, subclass it in a new file, `app/inputs/string_input.rb`:
|
546
|
+
|
547
|
+
```ruby
|
548
|
+
class StringInput < Formtastic::Inputs::StringInput
|
549
|
+
def to_html
|
550
|
+
puts "this is my modified version of StringInput"
|
551
|
+
super
|
552
|
+
end
|
553
|
+
end
|
554
|
+
```
|
555
|
+
|
556
|
+
Another way to modify behavior is by using the input generator:
|
557
|
+
```shell
|
558
|
+
$ rails generate formtastic:input string --extend
|
559
|
+
```
|
560
|
+
|
561
|
+
This generates the file `app/inputs/string_input.rb` with its respective content class.
|
562
|
+
|
563
|
+
You can use your modified version with `:as => :string`.
|
564
|
+
|
565
|
+
### Creating New Inputs Based on Existing Ones
|
566
|
+
|
567
|
+
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`:
|
568
|
+
|
569
|
+
```ruby
|
570
|
+
class FlexibleTextInput < Formtastic::Inputs::StringInput
|
571
|
+
def input_html_options
|
572
|
+
super.merge(:class => "flexible-text-area")
|
573
|
+
end
|
574
|
+
end
|
575
|
+
```
|
576
|
+
|
577
|
+
You can also extend existing input behavior by using the input generator:
|
578
|
+
|
579
|
+
```shell
|
580
|
+
$ rails generate formtastic:input FlexibleText --extend string
|
581
|
+
```
|
582
|
+
|
583
|
+
This generates the file `app/inputs/flexible_text_input.rb` with its respective content class.
|
584
|
+
|
585
|
+
You can use your new input with `:as => :flexible_text`.
|
586
|
+
|
587
|
+
### Creating New Inputs From Scratch
|
588
|
+
|
589
|
+
To create a custom `DatePickerInput` from scratch, put the following in `app/inputs/date_picker_input.rb`:
|
590
|
+
|
591
|
+
```ruby
|
592
|
+
class DatePickerInput
|
593
|
+
include Formtastic::Inputs::Base
|
594
|
+
def to_html
|
595
|
+
# ...
|
596
|
+
end
|
597
|
+
end
|
598
|
+
```
|
599
|
+
|
600
|
+
You can use your new input with `:as => :date_picker`.
|
601
|
+
|
602
|
+
|
603
|
+
## Dependencies
|
604
|
+
|
605
|
+
There are none other than Rails itself, but...
|
606
|
+
|
607
|
+
* 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.
|
608
|
+
* There are a bunch of development dependencies if you plan to contribute to Formtastic
|
609
|
+
|
610
|
+
|
611
|
+
## How to contribute
|
612
|
+
|
613
|
+
* Fork the project on Github
|
614
|
+
* Install development dependencies (`bundle install` and `bin/appraisal install`)
|
615
|
+
* Create a topic branch for your changes
|
616
|
+
* Ensure that you provide *documentation* and *test coverage* for your changes (patches won't be accepted without)
|
617
|
+
* Ensure that all tests pass (`bundle exec rake`)
|
618
|
+
* Create a pull request on Github (these are also a great place to start a conversation around a patch as early as possible)
|
619
|
+
|
620
|
+
|
621
|
+
## Project Info
|
622
|
+
|
623
|
+
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.
|
624
|
+
|
625
|
+
The project is hosted on Github: [https://github.com/justinfrench/formtastic](https://github.com/justinfrench/formtastic), where your contributions, forkings, comments, issues and feedback are greatly welcomed.
|
626
|
+
|
627
|
+
Copyright (c) 2007-2016 Justin French, released under the MIT license.
|
628
|
+
|