form_input 0.9.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +93 -68
- data/form_input.gemspec +2 -2
- data/lib/form_input/version.rb +2 -2
- metadata +10 -9
- data/.codeclimate.yml +0 -19
- data/.gitignore +0 -5
- data/.rubocop.yml +0 -1161
- data/.travis.gemfile +0 -15
- data/.travis.yml +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bf25959262e5eb0029d1185a5c7a9901a43cd16
|
4
|
+
data.tar.gz: 678589bac2e0bdcf52ca76568d63ee18cde7e128
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2d7d5d8caa65943d9765cf480f706614f7e77ad67766d5dddddb9f66f0f5f33c7d547aa614e648a5c43bf855c512d9764ea00988eafd3769ca5864733b7e281
|
7
|
+
data.tar.gz: fec7c3c8f5114861fefd20ed438a000bdab38f0c5bc912dd8a8a2d335e71c899b7ad22066b40598b1bf36ba3c1b0de1aeed87a5e8e528b4105655188db678f17
|
data/README.md
CHANGED
@@ -62,6 +62,9 @@ validating it, and making it available in a model-like structure.
|
|
62
62
|
The provided template snippets will take care of rendering the form parameters
|
63
63
|
as well as any errors detected back to the user.
|
64
64
|
You just get to use the input and control the flow the way you want.
|
65
|
+
In fact, it's not limited to form input only either -
|
66
|
+
it can be used with any web request input,
|
67
|
+
including that of [AJAX] or [REST] API end points.
|
65
68
|
The gem is completely framework agnostic,
|
66
69
|
comes with full test coverage,
|
67
70
|
and even supports multi-step forms and localization out of the box.
|
@@ -100,10 +103,10 @@ Sounds cool enough? Then read on.
|
|
100
103
|
* [Grouped Parameters](#grouped-parameters)
|
101
104
|
* [Chunked Parameters](#chunked-parameters)
|
102
105
|
* [Multi-Step Forms](#multi-step-forms)
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
106
|
+
* [Defining Multi-Step Forms](#defining-multi-step-forms)
|
107
|
+
* [Multi-Step Form Functionality](#multi-step-form-functionality)
|
108
|
+
* [Using Multi-Step Forms](#using-multi-step-forms)
|
109
|
+
* [Rendering Multi-Step Forms](#rendering-multi-step-forms)
|
107
110
|
* [Localization](#localization)
|
108
111
|
* [Error Messages and Inflection](#error-messages-and-inflection)
|
109
112
|
* [Localizing Forms](#localizing-forms)
|
@@ -112,7 +115,7 @@ Sounds cool enough? Then read on.
|
|
112
115
|
* [Inflection Filter](#inflection-filter)
|
113
116
|
* [Localizing Form Steps](#localizing-form-steps)
|
114
117
|
* [Supported Locales](#supported-locales)
|
115
|
-
|
118
|
+
* [Credits](#credits)
|
116
119
|
|
117
120
|
## Form Basics
|
118
121
|
|
@@ -121,9 +124,16 @@ what's the difference between internal and external representation,
|
|
121
124
|
how to create form instances and how to deal with errors,
|
122
125
|
and, finally, how to access the form input itself.
|
123
126
|
|
127
|
+
And note that while forms get mentioned a lot,
|
128
|
+
it can be all applied to any other web request input just as well.
|
129
|
+
Even [AJAX] end points or [REST] API end points
|
130
|
+
can describe their input and let it have converted and validated for them
|
131
|
+
by the same means,
|
132
|
+
which is worth keeping in mind.
|
133
|
+
|
124
134
|
### Defining Parameters
|
125
135
|
|
126
|
-
To
|
136
|
+
To define a form, simply inherit from `FormInput` and then
|
127
137
|
use the `param` or `param!` methods to define form parameters like this:
|
128
138
|
|
129
139
|
``` ruby
|
@@ -143,12 +153,12 @@ The _name_ is how you will address the parameter in your code,
|
|
143
153
|
while the optional _title_ is the string which will be displayed to the user by default.
|
144
154
|
|
145
155
|
The `param!` method works the same way but creates a required parameter.
|
146
|
-
Such parameters are required to appear in the input and have non-empty value.
|
156
|
+
Such parameters are required to appear in the input and to have a non-empty value.
|
147
157
|
Failure to do so will be automatically reported as an error
|
148
158
|
(discussed further in [Errors and Validation](#errors-and-validation)).
|
149
159
|
|
150
|
-
Both methods actually take
|
151
|
-
The _options_ is a hash which is used to control
|
160
|
+
Both methods actually take an optional _options_ as their last argument, too.
|
161
|
+
The _options_ argument is a hash which is used to control
|
152
162
|
most aspects of the parameter.
|
153
163
|
In fact, using the _title_ argument is just a shortcut identical to
|
154
164
|
setting the parameter option `:title` to the same value.
|
@@ -164,9 +174,9 @@ Parameters support many more parameter options,
|
|
164
174
|
and we will discuss each in turn as we go.
|
165
175
|
Comprehensive summary for an avid reader is however available in [Parameter Options](#parameter-options).
|
166
176
|
|
167
|
-
The value of each parameter is a string by default (or `nil` if the parameter is not set).
|
177
|
+
The value of each parameter is a string by default (or `nil` if the parameter is not set at all).
|
168
178
|
The string size is implicitly limited to 255 characters and bytes by default.
|
169
|
-
To limit the size
|
179
|
+
To limit the size explicitly, you can use an optional _size_ parameter like this:
|
170
180
|
|
171
181
|
``` ruby
|
172
182
|
param! :title, "Title", 100
|
@@ -213,7 +223,7 @@ We could do that by simply setting the `:filter` option to `nil`.
|
|
213
223
|
However, at the same time we want to get rid of the trailing newline character
|
214
224
|
which is often appended by the browser
|
215
225
|
when the user cuts and pastes the password from somewhere.
|
216
|
-
Not doing this would make the password fail for no apparent reason,
|
226
|
+
Not doing this would make the password eventually fail for no apparent reason,
|
217
227
|
resulting in poor user experience.
|
218
228
|
That's why we use `chomp` as the filter above.
|
219
229
|
The filter block is executed in the context of the string value itself,
|
@@ -237,7 +247,7 @@ and it will be called to obtain the actual value.
|
|
237
247
|
The block is called in context of the form parameter itself,
|
238
248
|
so it can access any of its methods and its form's methods easily.
|
239
249
|
For example, you can let the form automatically disable some fields
|
240
|
-
based on available user permissions by defining `is_forbidden?` accordingly:
|
250
|
+
based on available user permissions by defining the `is_forbidden?` method accordingly:
|
241
251
|
|
242
252
|
``` ruby
|
243
253
|
param :avatar, "Avatar",
|
@@ -255,7 +265,7 @@ you can factor them out and share them like this:
|
|
255
265
|
param :comment, "Comment", FEATURED_ARGS, type: :textarea
|
256
266
|
```
|
257
267
|
|
258
|
-
This works since you can actually pass several hashes in place of the _options_
|
268
|
+
This works since you can actually pass several hashes in place of the _options_ argument
|
259
269
|
and they all get merged together from left to right.
|
260
270
|
This allows you to mix various options presets together and then tweak them further as needed.
|
261
271
|
|
@@ -270,7 +280,7 @@ The internal representation, as you might have guessed,
|
|
270
280
|
are the parameter values which you will use in your application.
|
271
281
|
The external representation is how the parameters are present
|
272
282
|
to the browser via HTML forms or URLs
|
273
|
-
and passed back to the
|
283
|
+
and passed back to the server.
|
274
284
|
|
275
285
|
Normally, both representations are the same.
|
276
286
|
The parameters are named the same way in both cases
|
@@ -376,6 +386,9 @@ The complete filter for converting numbers to integers should thus look like thi
|
|
376
386
|
class: Integer
|
377
387
|
```
|
378
388
|
|
389
|
+
You can even consider using `strip.empty?`
|
390
|
+
if you want to allow an all-whitespace input to be consumed silently.
|
391
|
+
|
379
392
|
Of course, all that would be a lot of typing for something as common as integer parameters.
|
380
393
|
That's why the `FormInput` class comes with plenty standard filters predefined:
|
381
394
|
|
@@ -416,14 +429,15 @@ But you can easily change this by providing your own `:format` filter instead:
|
|
416
429
|
The provided block will be called in the context of the parameter value itself
|
417
430
|
and its result will be passed to the `to_s` conversion to create the final external value.
|
418
431
|
|
419
|
-
|
432
|
+
The use of a formatter is more than just mere cosmetics.
|
420
433
|
You will often use the formatter to complement your input filter.
|
421
|
-
For example, this is one possible way how to map arbitrary external values
|
434
|
+
For example, this is one possible way of how to map arbitrary external values
|
422
435
|
to their internal representation and back:
|
423
436
|
|
424
437
|
``` ruby
|
425
438
|
SORT_MODES = { id: 'n', views: 'v', age: 'a', likes: 'l' }
|
426
439
|
SORT_MODE_PARAMETERS = SORT_MODES.invert
|
440
|
+
|
427
441
|
param :sort_mode, :s,
|
428
442
|
filter: ->{ SORT_MODE_PARAMETERS[ self ] || self },
|
429
443
|
format: ->{ SORT_MODES[ self ] },
|
@@ -433,7 +447,7 @@ to their internal representation and back:
|
|
433
447
|
Note that once again the original value is preserved in case of error,
|
434
448
|
so it can be passed back to the user for fixing.
|
435
449
|
|
436
|
-
Another example shows how to process credit card expiration field:
|
450
|
+
Another example shows how to process a credit card expiration field:
|
437
451
|
|
438
452
|
``` ruby
|
439
453
|
EXPIRY_ARGS = {
|
@@ -488,7 +502,7 @@ So, what's the difference between `:filter` and `:transform`?
|
|
488
502
|
For scalar values, like normal string or integer parameters, there is none.
|
489
503
|
In that case the `:transform` is just an additional filter,
|
490
504
|
and you are free to use either or both.
|
491
|
-
But `FormInput` class supports
|
505
|
+
But `FormInput` class also supports array and hash parameters,
|
492
506
|
as we will learn in the very next chapter,
|
493
507
|
and that's where it makes the difference.
|
494
508
|
The input filter is used to convert each individual element,
|
@@ -525,7 +539,7 @@ rather than becoming an empty array.
|
|
525
539
|
|
526
540
|
All the parameter options of scalar parameters can be used with array parameters as well.
|
527
541
|
In this case, however, they apply to the individual elements of the array.
|
528
|
-
The array parameters
|
542
|
+
The array parameters additionally support the `:min_count` and `:max_count` options,
|
529
543
|
which restrict the number of elements the array can have.
|
530
544
|
For example, to limit the keywords both in string size and element count, you can do this:
|
531
545
|
|
@@ -539,7 +553,7 @@ whereas the input `:transform` is applied to the array as a whole.
|
|
539
553
|
For example, to get sorted array of integers you can do this:
|
540
554
|
|
541
555
|
``` ruby
|
542
|
-
array :ids, INTEGER_ARGS, transform: ->{ compact.sort }
|
556
|
+
array :ids, INTEGER_ARGS, transform: ->{ compact.sort rescue self }
|
543
557
|
```
|
544
558
|
|
545
559
|
The `compact` method above takes care of removing any unfilled entries from the array prior sorting.
|
@@ -571,18 +585,17 @@ which should specify a regular expression
|
|
571
585
|
which all hash keys must match.
|
572
586
|
This may not be the wisest move, but it's your call.
|
573
587
|
Just make sure you use the `\A` and `\z` anchors rather than `^` and `$`,
|
574
|
-
so you don't leave yourself open to nasty
|
588
|
+
so you don't leave yourself open to nasty surprises.
|
575
589
|
|
576
|
-
While practical use of hash parameters with forms is
|
577
|
-
so you will most likely only use them with URL based non-form input, if ever,
|
590
|
+
While practical use of hash parameters with forms is relatively limited,
|
578
591
|
the array parameters are pretty common.
|
579
|
-
The examples above could be used for gathering list of input fields into single array,
|
592
|
+
The examples above could be used for gathering list of input fields into a single array,
|
580
593
|
which is useful as well,
|
581
594
|
but the most common use of array parameters is for multi-select or multi-checkbox fields.
|
582
595
|
|
583
596
|
To declare a select parameter, you can set the `:type` to `:select` and
|
584
597
|
use the `:data` option to provide an array of values for the select menu.
|
585
|
-
The array contains pairs of parameter values to use and the
|
598
|
+
The array contains pairs of parameter values to use and the corresponding text to show to the user.
|
586
599
|
For example, using a [Sequel]-like `Country` model:
|
587
600
|
|
588
601
|
``` ruby
|
@@ -999,7 +1012,7 @@ All this ensures you automatically get consistent validation results anytime you
|
|
999
1012
|
The only exception is when you set the parameter values explicitly using their setter methods.
|
1000
1013
|
This intentionally leaves the errors reported intact,
|
1001
1014
|
allowing you to adjust the parameter values
|
1002
|
-
without
|
1015
|
+
without interfering with the validation results.
|
1003
1016
|
Which finally brings us to the topic of accessing the parameter values themselves.
|
1004
1017
|
|
1005
1018
|
### Using Forms
|
@@ -1168,7 +1181,7 @@ The following methods are available:
|
|
1168
1181
|
form.incorrect_params # Parameters whose current value doesn't match their kind.
|
1169
1182
|
```
|
1170
1183
|
|
1171
|
-
Each of them simply selects the
|
1184
|
+
Each of them simply selects the parameters using their boolean getter of the same name.
|
1172
1185
|
Each of them is available in the `*_parameters` form for as well,
|
1173
1186
|
for those who don't like the `params` shortcut.
|
1174
1187
|
|
@@ -1216,7 +1229,7 @@ What you use this for is up to you.
|
|
1216
1229
|
We will see later that for example the [Multi-Step Forms](#multi-step-forms) use this for grouping parameters which belong to individual steps,
|
1217
1230
|
but it has plenty other uses as well.
|
1218
1231
|
|
1219
|
-
|
1232
|
+
### URL Helpers
|
1220
1233
|
|
1221
1234
|
The `FormInput` is primarily intended for use with HTML forms,
|
1222
1235
|
which we will discuss in detail in the [Form Templates](#form-templates) chapter,
|
@@ -1295,7 +1308,7 @@ Finally, if you do not like the idea of parameter arrays in your URLs, you can u
|
|
1295
1308
|
Just note that none of the standard array parameter validations apply in this case,
|
1296
1309
|
so make sure to apply your own validations using the `:check` callback if you need to.
|
1297
1310
|
|
1298
|
-
|
1311
|
+
### Form Helpers
|
1299
1312
|
|
1300
1313
|
It may come as a surprise, but `FormInput` provides no helpers for creating HTML tags.
|
1301
1314
|
That's because doing so would be a completely futile effort.
|
@@ -1356,7 +1369,7 @@ To illustrate all this, a select parameter can be rendered like this:
|
|
1356
1369
|
Finally, you will likely want to render the parameter name in some way.
|
1357
1370
|
For this, each parameter has the `form_title` method,
|
1358
1371
|
which returns the title to show in the form.
|
1359
|
-
It defaults to its title, but can be
|
1372
|
+
It defaults to its title, but can be overridden with the `:form_title` option.
|
1360
1373
|
If neither is set, the code name will be used instead.
|
1361
1374
|
To render it, you will use something like this:
|
1362
1375
|
|
@@ -1385,7 +1398,7 @@ you will eventually want to extend it further to better fit your project.
|
|
1385
1398
|
To do this, it's common to define the `Form` class inherited from `FormInput`,
|
1386
1399
|
put various helpers there, and base your own forms on that.
|
1387
1400
|
This is also the place where you can include your own `FormInput` types extensions.
|
1388
|
-
This chapter
|
1401
|
+
This chapter shows some ideas you may want to built upon to get you started.
|
1389
1402
|
|
1390
1403
|
Adding custom boolean getters which you may need:
|
1391
1404
|
|
@@ -1427,6 +1440,16 @@ Keeping track of additional form state:
|
|
1427
1440
|
end
|
1428
1441
|
```
|
1429
1442
|
|
1443
|
+
Adding support for your [own types](https://gist.github.com/raxoft/1e717d7dcaab6949ab03):
|
1444
|
+
|
1445
|
+
``` ruby
|
1446
|
+
MONEY_ARGS = {
|
1447
|
+
filter: ->{ ( Money( self ) rescue self ) unless empty? },
|
1448
|
+
format: ->{ Money( self ) },
|
1449
|
+
class: Money
|
1450
|
+
}
|
1451
|
+
```
|
1452
|
+
|
1430
1453
|
The list could go on and on, as everyone might need slightly different tweaks.
|
1431
1454
|
Eventually, though, you will come up with your own set of extensions which you will keep using across projects.
|
1432
1455
|
Once you do, consider sharing them with the rest of the world. Thanks.
|
@@ -1455,8 +1478,8 @@ This is a brief but comprehensive summary of all parameter options:
|
|
1455
1478
|
* `:data` - array containing data for parameter types which need one, like select, multi-select, or multi-checkbox.
|
1456
1479
|
Shall contain the allowed parameter values paired with the corresponding text to display in forms.
|
1457
1480
|
Defaults to empty array if not set.
|
1458
|
-
* `:tag` or `:tags` - arbitrary symbol or array of symbols used to tag the parameter with arbitrary
|
1459
|
-
See `tagged?` method in the [Using Forms](#using-forms) chapter.
|
1481
|
+
* `:tag` or `:tags` - arbitrary symbol or array of symbols used to tag the parameter with arbitrary semantics.
|
1482
|
+
See the `tagged?` method in the [Using Forms](#using-forms) chapter.
|
1460
1483
|
* `:filter` - callback used to cleanup or convert the input values.
|
1461
1484
|
See [Input Filter](#input-filter).
|
1462
1485
|
* `:transform` - optional callback used to further convert the input values.
|
@@ -1483,7 +1506,7 @@ This is a brief but comprehensive summary of all parameter options:
|
|
1483
1506
|
Defaults to 255.
|
1484
1507
|
* `:min_bytesize` - when set, value(s) of that parameter must have at least this many bytes.
|
1485
1508
|
* `:max_bytesize` - when set, value(s) of that parameter may have at most this many bytes.
|
1486
|
-
Defaults to 255 if `:max_size` is dynamic option or less than 256.
|
1509
|
+
Defaults to 255 if `:max_size` is a dynamic option or less than 256.
|
1487
1510
|
* `:reject` - regular expression (or array thereof) which the parameter value(s) may not match.
|
1488
1511
|
* `:reject_msg` - custom error message used when the `:reject` check fails.
|
1489
1512
|
Defaults to `:msg` message.
|
@@ -1501,11 +1524,11 @@ This is a brief but comprehensive summary of all parameter options:
|
|
1501
1524
|
* `:gender` - grammatical gender used for localization.
|
1502
1525
|
See [Localization](#localization) for details.
|
1503
1526
|
* `:row` - used for grouping several parameters together, usually to render them in a single row.
|
1504
|
-
See [Chunked
|
1527
|
+
See [Chunked Parameters](#chunked-parameters).
|
1505
1528
|
* `:cols` - optional custom option used to set span of the parameter in a single row.
|
1506
|
-
See [Chunked
|
1529
|
+
See [Chunked Parameters](#chunked-parameters).
|
1507
1530
|
* `:group` - custom option used for grouping parameters in arbitrary ways.
|
1508
|
-
See [Grouped
|
1531
|
+
See [Grouped Parameters](#grouped-parameters).
|
1509
1532
|
* `:size` - custom option used to set size of `:select` and `:textarea` parameters.
|
1510
1533
|
* `:subtitle` - custom option used to render an additional subtitle after the form title.
|
1511
1534
|
* `:placeholder` - custom option used for setting the placeholder attribute of the parameter.
|
@@ -1556,7 +1579,7 @@ Also note that you can find the example templates discussed here in the `form_in
|
|
1556
1579
|
### Form Template
|
1557
1580
|
|
1558
1581
|
To put the form on a page, you use the stock HTML `form` tag.
|
1559
|
-
The snippets will be used for rendering the form content,
|
1582
|
+
The snippets will be used for rendering of the form content,
|
1560
1583
|
but the form itself and the submission buttons used are usually form specific anyway,
|
1561
1584
|
so it is rarely worth factoring it out.
|
1562
1585
|
Assuming the controller passes the form to the view in the `@form` variable,
|
@@ -1704,7 +1727,7 @@ You may want to review it after you have seen them used in some context:
|
|
1704
1727
|
|
1705
1728
|
Having seen the basics, we are now ready to start expanding them towards more complex snippets.
|
1706
1729
|
|
1707
|
-
### Complex
|
1730
|
+
### Complex Parameters
|
1708
1731
|
|
1709
1732
|
Forms are often more than just few simple text input fields,
|
1710
1733
|
so it is necessary to render more complex parameters as well.
|
@@ -1784,7 +1807,7 @@ we use normal select for scalar parameters and multi-select for array parameters
|
|
1784
1807
|
The data to render comes from the `data` attribute of the parameter,
|
1785
1808
|
see the [Array and Hash Parameters](#array-and-hash-parameters) chapter for details.
|
1786
1809
|
Also note how the value is passed to the `selected?` method
|
1787
|
-
and how it is
|
1810
|
+
and how it is formatted by the `format_value` method.
|
1788
1811
|
|
1789
1812
|
#### Radio Buttons
|
1790
1813
|
|
@@ -1816,10 +1839,10 @@ The `selected?` and `format_value` methods are used the same way, too.
|
|
1816
1839
|
|
1817
1840
|
Checkboxes can be used in two ways.
|
1818
1841
|
You can either use them as individual on/off checkboxes,
|
1819
|
-
or use them as an alternative to
|
1842
|
+
or use them as an alternative to multi-select.
|
1820
1843
|
Their rendering follows this -
|
1821
1844
|
we use the on/off approach for scalar parameters,
|
1822
|
-
and the
|
1845
|
+
and the multi-select one for array parameters:
|
1823
1846
|
|
1824
1847
|
``` slim
|
1825
1848
|
- when :checkbox
|
@@ -1851,7 +1874,7 @@ and the multiselect one for array parameters:
|
|
1851
1874
|
= p[:text]
|
1852
1875
|
```
|
1853
1876
|
|
1854
|
-
As you can see, the
|
1877
|
+
As you can see, the multi-select case is basically identical to rendering of radio buttons,
|
1855
1878
|
only the input type attribute changes.
|
1856
1879
|
For on/off checkboxes, though, there are more changes.
|
1857
1880
|
|
@@ -1868,7 +1891,7 @@ like any other parameter option it can be evaluated at runtime if needed,
|
|
1868
1891
|
see [Defining Parameters](#defining-parameters) for details.
|
1869
1892
|
And the [Localization](#localization) chapter will explain how to get the text localized easily.
|
1870
1893
|
|
1871
|
-
### Inflatable
|
1894
|
+
### Inflatable Parameters
|
1872
1895
|
|
1873
1896
|
We have seen how to render array parameters as multi-select or multi-checkbox fields.
|
1874
1897
|
Sometimes, however, you really want to render them as an array of text input fields.
|
@@ -1908,9 +1931,9 @@ which will just update the form,
|
|
1908
1931
|
in addition to the standard submit button.
|
1909
1932
|
This button will allow the user to add as many items as needed before submitting the form.
|
1910
1933
|
|
1911
|
-
### Extending
|
1934
|
+
### Extending Parameters
|
1912
1935
|
|
1913
|
-
The examples above show rendering of parameters which shall take care of most your needs,
|
1936
|
+
The examples above show rendering of parameters which shall take care of most of your needs,
|
1914
1937
|
but it doesn't need to end there. Do you need some extra functionality?
|
1915
1938
|
It's trivial to pass additional parameter options along and
|
1916
1939
|
render them in the template the way you need.
|
@@ -1960,7 +1983,7 @@ You can put them in a hash and add them using a splat operator like this:
|
|
1960
1983
|
And so on and on.
|
1961
1984
|
As you can see, the templates make it really easy to adjust them any way you may need.
|
1962
1985
|
|
1963
|
-
### Grouped
|
1986
|
+
### Grouped Parameters
|
1964
1987
|
|
1965
1988
|
Sometimes you may want to group some parameters together and render the groups accordingly,
|
1966
1989
|
say in their own subframe.
|
@@ -1985,7 +2008,7 @@ The example above uses the `group_name` method which is supposed to return the n
|
|
1985
2008
|
You would have to provide that,
|
1986
2009
|
but check out the [Localization Helpers](#localization-helpers) chapter for a tip how it can be done easily.
|
1987
2010
|
|
1988
|
-
### Chunked
|
2011
|
+
### Chunked Parameters
|
1989
2012
|
|
1990
2013
|
Occasionally,
|
1991
2014
|
you may want to render some input fields on the same line.
|
@@ -2112,7 +2135,7 @@ class PostForm < FormInput
|
|
2112
2135
|
param :zip, "ZIP Code", tag: :address
|
2113
2136
|
|
2114
2137
|
param! :message, "Your Message", tag: :message
|
2115
|
-
param :comment, "Optional
|
2138
|
+
param :comment, "Optional Comment", tag: :message
|
2116
2139
|
|
2117
2140
|
param :url, type: :hidden
|
2118
2141
|
end
|
@@ -2127,7 +2150,7 @@ the user starts at the first step and finishes at the last
|
|
2127
2150
|
(at least that's the most typical flow).
|
2128
2151
|
|
2129
2152
|
The `:tag` option is then used to assign each parameter to particular step.
|
2130
|
-
If you would need additional tags, remember that you can use the `:tags` array as well.
|
2153
|
+
If you would need additional tags, remember that you can use the `:tags` array instead as well.
|
2131
2154
|
Also note that the hidden parameter doesn't have to belong to any step,
|
2132
2155
|
as it will be always rendered as a hidden field anyway.
|
2133
2156
|
|
@@ -2141,7 +2164,7 @@ Similarly, you could have an initial `:intro` step or some other intermediate st
|
|
2141
2164
|
The final `:post` step serves as a terminator which we will use to actually process the form data.
|
2142
2165
|
Note that it doesn't have a name,
|
2143
2166
|
so it won't appear among the list of step names if we decide to display them somewhere
|
2144
|
-
(see [Rendering Multi-Step
|
2167
|
+
(see [Rendering Multi-Step Forms](#rendering-multi-step-forms) for example of that).
|
2145
2168
|
|
2146
2169
|
We will soon delve into how to use such forms, but first let's discuss their enhanced functionality.
|
2147
2170
|
|
@@ -2155,12 +2178,12 @@ The most important of those parameters is the `step` parameter.
|
|
2155
2178
|
It is always set to the name of the current step,
|
2156
2179
|
starting with the first step defined by default.
|
2157
2180
|
If you need to, you can change the current step by simply assigning to it,
|
2158
|
-
we will see examples of that later in the [Using Multi-Step
|
2181
|
+
we will see examples of that later in the [Using Multi-Step Forms](#using-multi-step-forms) chapter.
|
2159
2182
|
|
2160
2183
|
The second important parameter is the `next` parameter.
|
2161
2184
|
It contains the desired step which the user wants to go to whenever he posts the form.
|
2162
2185
|
This parameter is used to let the user progress throughout the form -
|
2163
|
-
we will see examples of that in the [Rendering Multi-Step
|
2186
|
+
we will see examples of that in the [Rendering Multi-Step Forms](#rendering-multi-step-forms) chapter.
|
2164
2187
|
If it is set and there are no problems with the parameters of the currently submitted step,
|
2165
2188
|
it will be used to update the value of the `step` parameter,
|
2166
2189
|
effectively changing the current step to whatever the user wanted.
|
@@ -2172,8 +2195,8 @@ The `seen` parameter contains the highest step among the steps seen by the user
|
|
2172
2195
|
Unlike the `last` parameter, which is always set, the `seen` parameter can be `nil` if no steps were displayed before yet.
|
2173
2196
|
The current step is not included when it is displayed for the first time,
|
2174
2197
|
it will become included only if it is displayed more than once.
|
2175
|
-
Neither of these parameters is used directly
|
2176
|
-
|
2198
|
+
Neither of these parameters is usually used directly, though.
|
2199
|
+
Instead, they are used by several helper methods for classifying the already visited steps,
|
2177
2200
|
which we will see shortly.
|
2178
2201
|
|
2179
2202
|
There are three methods added which extend the list of methods
|
@@ -2298,7 +2321,7 @@ from all these methods (except the `extra_steps` method itself, of course):
|
|
2298
2321
|
```
|
2299
2322
|
|
2300
2323
|
The first of the incorrect steps is of particular interest,
|
2301
|
-
so there is
|
2324
|
+
so there is a shortcut method `incorrect_step` just for that:
|
2302
2325
|
|
2303
2326
|
``` ruby
|
2304
2327
|
form.incorrect_step # :email
|
@@ -2464,7 +2487,7 @@ See how the submit button uses the `next` value to proceed to the next step.
|
|
2464
2487
|
Without it, the form would be merely updated when submitted.
|
2465
2488
|
|
2466
2489
|
Also note how we control when to display the errors -
|
2467
|
-
we use the `finished_step?` method to
|
2490
|
+
we use the `finished_step?` method to suppress the errors whenever
|
2468
2491
|
the user sees certain step for the first time.
|
2469
2492
|
|
2470
2493
|
Note that it is also possible to divert the form rendering for individual steps if you need to.
|
@@ -2512,7 +2535,7 @@ To allow users to go back to the previous step, prepend something like this:
|
|
2512
2535
|
```
|
2513
2536
|
|
2514
2537
|
Note that browsers nowadays automatically use the first submit button when the user hits the enter in the text field.
|
2515
|
-
If you have multiple buttons in the form and the first one is not
|
2538
|
+
If you have multiple buttons in the form and the first one is not guaranteed to be the one you want,
|
2516
2539
|
you can add the following invisible button as the first button in the form to make the browser go to the step you want:
|
2517
2540
|
|
2518
2541
|
``` slim
|
@@ -2654,7 +2677,7 @@ the [Inflection Filter](#inflection-filter) chapter will explain the gory detail
|
|
2654
2677
|
For English, the inflection rules are pretty simple.
|
2655
2678
|
You only need to distinguish between singular and plural grammatical number.
|
2656
2679
|
By default,
|
2657
|
-
`FormInput` uses
|
2680
|
+
`FormInput` uses singular for scalar parameters and plural for array and hash parameters.
|
2658
2681
|
With localization enabled,
|
2659
2682
|
you can override it by setting the `:plural` parameter option to `true` or `'p'` for plural,
|
2660
2683
|
and to `false` or `'s'` for singular, respectively:
|
@@ -2840,7 +2863,7 @@ forms:
|
|
2840
2863
|
Note the use of the `:gender` option to get properly inflected error messages.
|
2841
2864
|
|
2842
2865
|
Now let's say we decide to use a custom error message when the user forgets to fill in the content of the message field.
|
2843
|
-
To do this, we add the value of the `:required_msg` option of the `message` parameter directly to `en.yml` like this:
|
2866
|
+
To do this, we add the value of the `:required_msg` option of the `message` parameter directly to the `en.yml` file like this:
|
2844
2867
|
|
2845
2868
|
``` yaml
|
2846
2869
|
message:
|
@@ -2849,7 +2872,7 @@ To do this, we add the value of the `:required_msg` option of the `message` para
|
|
2849
2872
|
```
|
2850
2873
|
|
2851
2874
|
Note that this works even if you don't add the `:required_msg` option to the parameter
|
2852
|
-
within the `ContactForm` definition itself.
|
2875
|
+
within the `ContactForm` class definition itself.
|
2853
2876
|
That's because as long as you have the locale support enabled,
|
2854
2877
|
all applicable parameter options are automatically looked up in the translation files first.
|
2855
2878
|
If the corresponding translation is found,
|
@@ -2864,7 +2887,7 @@ However note that R18n normally provides its own default translation based on it
|
|
2864
2887
|
usually falling back to English in the end.
|
2865
2888
|
This means that once you add some translation to the `en.yml` file,
|
2866
2889
|
the parameter option will get this English translation for all other locales as well,
|
2867
|
-
until you add the corresponding translation
|
2890
|
+
until you add the corresponding translation to the other translations files as well.
|
2868
2891
|
|
2869
2892
|
So, to make sure we get the Czech version of the `:required_msg` for the example above,
|
2870
2893
|
the `cs.yml` file should be updated as well like this:
|
@@ -2953,7 +2976,7 @@ rather than the global namespace.
|
|
2953
2976
|
```
|
2954
2977
|
|
2955
2978
|
Like the `ft` method, it provides three syntax alternatives for getting the desired translation.
|
2956
|
-
It's worth mentioning that the `()` syntax automatically appends the parameter itself as the last
|
2979
|
+
It's worth mentioning that the `()` syntax automatically appends the parameter itself as the last argument,
|
2957
2980
|
making it available for the [Inflection Filter](#inflection-filter), which will be discussed later.
|
2958
2981
|
|
2959
2982
|
The `pt` method is typically used in parameter callbacks and
|
@@ -3005,7 +3028,7 @@ like this:
|
|
3005
3028
|
|
3006
3029
|
In either case,
|
3007
3030
|
if set correctly,
|
3008
|
-
the inflection filter will pick up the last argument provided and choose the
|
3031
|
+
the inflection filter will pick up the last argument provided and choose the appropriately inflected message.
|
3009
3032
|
Now let's see how exactly is this done.
|
3010
3033
|
|
3011
3034
|
### Inflection Filter
|
@@ -3035,7 +3058,7 @@ used to choose the desired translation.
|
|
3035
3058
|
Now what is this inflection string and where does it come from?
|
3036
3059
|
|
3037
3060
|
Each parameter has the `inflection` method which returns
|
3038
|
-
the desired string used to choose the
|
3061
|
+
the desired string used to choose the appropriately inflected error message.
|
3039
3062
|
By default, it returns the grammatical number and grammatical gender strings combined,
|
3040
3063
|
as returned by the `plural` and `gender` methods of the parameter, respectively.
|
3041
3064
|
If needed, it can be also set directly by the `:inflect` parameter option.
|
@@ -3070,7 +3093,7 @@ Here is an excerpt from the Czech translation file which demonstrates this:
|
|
3070
3093
|
```
|
3071
3094
|
|
3072
3095
|
As you can see, there are three distinct variants in the singular case, one for each gender.
|
3073
|
-
The plural case on the other hand
|
3096
|
+
The plural case on the other hand uses the same variant for most genders, with only two specific exceptions defined.
|
3074
3097
|
|
3075
3098
|
Defining translations like this may seem complex,
|
3076
3099
|
but it should feel fairly natural to people fluent in given language.
|
@@ -3107,7 +3130,7 @@ would look like:
|
|
3107
3130
|
message:
|
3108
3131
|
title: Your Message
|
3109
3132
|
comment:
|
3110
|
-
title: Optional
|
3133
|
+
title: Optional Comment
|
3111
3134
|
steps:
|
3112
3135
|
email: Email
|
3113
3136
|
name: Name
|
@@ -3151,6 +3174,8 @@ The `FormInput` is released under the MIT license.
|
|
3151
3174
|
|
3152
3175
|
|
3153
3176
|
[DSL]: http://en.wikipedia.org/wiki/Domain-specific_language
|
3177
|
+
[REST]: https://en.wikipedia.org/wiki/Representational_state_transfer
|
3178
|
+
[AJAX]: https://en.wikipedia.org/wiki/Ajax_(programming)
|
3154
3179
|
[Sinatra]: http://www.sinatrarb.com/
|
3155
3180
|
[Ramaze]: http://ramaze.net/
|
3156
3181
|
[Slim]: https://github.com/slim-template/slim
|