sinatra-formkeeper 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/README.md +209 -26
- data/lib/sinatra/formkeeper/version.rb +1 -1
- metadata +2 -2
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -42,13 +42,13 @@ end
|
|
42
42
|
|
43
43
|
### 0: Preparation
|
44
44
|
|
45
|
-
At your application file's header, add
|
45
|
+
At your application file's header, add `require` line for this library.
|
46
46
|
|
47
47
|
```ruby
|
48
48
|
require 'sinatra/formkeeper'
|
49
49
|
```
|
50
50
|
|
51
|
-
And if your application is Sinatra::Base inheritance type, register Sinatra::FormKeeper
|
51
|
+
And if your application is `Sinatra::Base` inheritance type, register `Sinatra::FormKeeper`
|
52
52
|
|
53
53
|
```ruby
|
54
54
|
class MyApp < Sinatra::Base
|
@@ -73,13 +73,13 @@ post '/entry' do
|
|
73
73
|
end
|
74
74
|
```
|
75
75
|
|
76
|
-
Calling
|
76
|
+
Calling `form` with block which includes rule-setting,
|
77
77
|
you can build a form-rule.
|
78
|
-
There are some DSL-method to build rules. In this example,
|
78
|
+
There are some DSL-method to build rules. In this example, `filters` and `field` are written.
|
79
79
|
|
80
80
|
#### filters
|
81
81
|
|
82
|
-
You can set
|
82
|
+
You can set `filters`. All input parameters are filtered by indicated filtering feature
|
83
83
|
The filtering process is executed before validation.
|
84
84
|
|
85
85
|
```ruby
|
@@ -98,7 +98,7 @@ form do
|
|
98
98
|
end
|
99
99
|
```
|
100
100
|
|
101
|
-
All preset filters are described at [8: Preset Filters]
|
101
|
+
All preset filters are described at [8: Preset Filters](#8-preset-filters)
|
102
102
|
|
103
103
|
#### field
|
104
104
|
|
@@ -111,7 +111,7 @@ form do
|
|
111
111
|
end
|
112
112
|
```
|
113
113
|
|
114
|
-
This constraint works for an input form named as
|
114
|
+
This constraint works for an input form named as `field_name`, for instance
|
115
115
|
|
116
116
|
```html
|
117
117
|
<input type="text" name="field_name" />
|
@@ -121,11 +121,11 @@ And key-value pares are following the field name.
|
|
121
121
|
They are constraints set for the field.
|
122
122
|
You can add your favorite constraints here.
|
123
123
|
|
124
|
-
All preset constraints are described at [9: Preset Constraints]
|
124
|
+
All preset constraints are described at [9: Preset Constraints](#9-preset-constraints)
|
125
125
|
Read the chapter for more detail.
|
126
126
|
|
127
|
-
|
128
|
-
set
|
127
|
+
`:present` is a special constraint. if parameter not found for the field which
|
128
|
+
set `:present` constraint, the field will be marked as *not present*,
|
129
129
|
and other validation for rest constraints won't be executed.
|
130
130
|
|
131
131
|
You also can set :default
|
@@ -140,11 +140,11 @@ end
|
|
140
140
|
When it's set, if parameter not found, the indicated value will be set
|
141
141
|
and other validation for rest constraints won't be executed.
|
142
142
|
|
143
|
-
You aren't allowed to set both
|
143
|
+
You aren't allowed to set both *:present* and *:default* at same time.
|
144
144
|
|
145
145
|
And you can set filters here,
|
146
146
|
if you don't want to filter all the parameters included in the request.
|
147
|
-
This filtering setting only affets on
|
147
|
+
This filtering setting only affets on `:field_name`.
|
148
148
|
|
149
149
|
```ruby
|
150
150
|
form do
|
@@ -196,12 +196,12 @@ Or
|
|
196
196
|
```
|
197
197
|
|
198
198
|
Rack request handle such type of name (exp: field_name[]) as Array.
|
199
|
-
For this type of input, use
|
200
|
-
In this case, you must use
|
199
|
+
For this type of input, use `selection` method.
|
200
|
+
In this case, you must use `:count` constraints instead of `:present`.
|
201
201
|
|
202
202
|
#### combination
|
203
203
|
|
204
|
-
There is another special rule,
|
204
|
+
There is another special rule, *Combination*
|
205
205
|
|
206
206
|
```ruby
|
207
207
|
form do
|
@@ -214,7 +214,7 @@ Set rule-name as a first argument.
|
|
214
214
|
And you should set multiple target fields.
|
215
215
|
And one constraint like (:same => true), or (:any => true).
|
216
216
|
|
217
|
-
|
217
|
+
`:same` and `:any` are called as *Combination Constraint*
|
218
218
|
For this purpose, formkeeper provides you a simple way to do same things.
|
219
219
|
|
220
220
|
```ruby
|
@@ -224,14 +224,14 @@ form do
|
|
224
224
|
end
|
225
225
|
```
|
226
226
|
|
227
|
-
You can call a name of
|
227
|
+
You can call a name of *Combination Constraints* as a method.
|
228
228
|
Followed by rule-name and target-fields.
|
229
229
|
|
230
|
-
All preset constraints are described at [10: Preset Combination Constraints]
|
230
|
+
All preset constraints are described at [10: Preset Combination Constraints](#10-preset-combination-constraints)
|
231
231
|
|
232
232
|
### 2: Check if user's input is valid or not
|
233
233
|
|
234
|
-
|
234
|
+
`form.failed?` can be used to judge if user's input is valid for the rule you build.
|
235
235
|
|
236
236
|
```ruby
|
237
237
|
post '/entry' do
|
@@ -251,9 +251,9 @@ end
|
|
251
251
|
After validation is proccessed without any failure,
|
252
252
|
you can implement your domain logic with valid parameters.
|
253
253
|
|
254
|
-
|
254
|
+
`form[:field_name]` can be used to pick up a valid data.
|
255
255
|
This data you can obtain through this method is a filtered data
|
256
|
-
according to the rule you build (if you set a
|
256
|
+
according to the rule you build (if you set a `filters` rule).
|
257
257
|
|
258
258
|
```ruby
|
259
259
|
post '/entry' do
|
@@ -273,7 +273,7 @@ end
|
|
273
273
|
|
274
274
|
When validation is failed, you might want to provide user
|
275
275
|
same form again, with error message that describes what fields was invalid.
|
276
|
-
For this purpose, use
|
276
|
+
For this purpose, use `failed_on?` method.
|
277
277
|
|
278
278
|
```ruby
|
279
279
|
post '/entry' do
|
@@ -310,7 +310,7 @@ __END__
|
|
310
310
|
|
311
311
|
### 5: Check if what field and constraint has failed?
|
312
312
|
|
313
|
-
You can pass constraint-type to
|
313
|
+
You can pass constraint-type to `failed_on?` as a second argument.
|
314
314
|
This provides you a way to show detailed error-messages.
|
315
315
|
|
316
316
|
```ruby
|
@@ -352,7 +352,7 @@ __END__
|
|
352
352
|
### 6: Fill in form
|
353
353
|
|
354
354
|
In many case you might want to fill in form with user's last input.
|
355
|
-
Do like following.
|
355
|
+
Do like following. `fill_in_form` automatically fill the fields with `params`
|
356
356
|
|
357
357
|
```ruby
|
358
358
|
post '/entry' do
|
@@ -389,7 +389,7 @@ DEFAULT:
|
|
389
389
|
-- ...
|
390
390
|
```
|
391
391
|
|
392
|
-
DEFAULT is a special type. If it can't find setting for indicated validation-type, it uses message set for DEFAULT
|
392
|
+
`DEFAULT` is a special type. If it can't find setting for indicated validation-type, it uses message set for `DEFAULT`.
|
393
393
|
After you prepare a yaml file, load it.
|
394
394
|
|
395
395
|
```ruby
|
@@ -451,19 +451,202 @@ If you want to show messages for each field, separately, of course you can.
|
|
451
451
|
|
452
452
|
### 9: Preset Constraints
|
453
453
|
|
454
|
-
#### present
|
455
454
|
#### length
|
455
|
+
|
456
|
+
calculate length. this constraint use String#length internally
|
457
|
+
You can set integer.
|
458
|
+
|
459
|
+
```ruby
|
460
|
+
post '/entry' do
|
461
|
+
form do
|
462
|
+
field :field01, :present => true, :length => 10
|
463
|
+
end
|
464
|
+
#...
|
465
|
+
end
|
466
|
+
```
|
467
|
+
|
468
|
+
Or as range
|
469
|
+
|
470
|
+
```ruby
|
471
|
+
post '/entry' do
|
472
|
+
form do
|
473
|
+
field :field01, :present => true, :length => 4..10
|
474
|
+
end
|
475
|
+
#...
|
476
|
+
end
|
477
|
+
```
|
478
|
+
|
456
479
|
#### bytesize
|
480
|
+
|
481
|
+
Calculate byte size. this constraint use String#bytesize internally
|
482
|
+
You can set integer.
|
483
|
+
|
484
|
+
```ruby
|
485
|
+
post '/entry' do
|
486
|
+
form do
|
487
|
+
field :field01, :present => true, :bytesize => 10
|
488
|
+
end
|
489
|
+
#...
|
490
|
+
end
|
491
|
+
```
|
492
|
+
|
493
|
+
Or as range
|
494
|
+
|
495
|
+
```ruby
|
496
|
+
post '/entry' do
|
497
|
+
form do
|
498
|
+
field :field01, :present => true, :bytesize => 4..10
|
499
|
+
end
|
500
|
+
#...
|
501
|
+
end
|
502
|
+
```
|
503
|
+
|
457
504
|
#### ascii
|
505
|
+
|
506
|
+
```ruby
|
507
|
+
post '/entry' do
|
508
|
+
form do
|
509
|
+
field :field01, :present => true, :ascii => true
|
510
|
+
end
|
511
|
+
#...
|
512
|
+
end
|
513
|
+
```
|
514
|
+
|
458
515
|
#### regexp
|
516
|
+
|
517
|
+
```ruby
|
518
|
+
post '/entry' do
|
519
|
+
form do
|
520
|
+
field :field01, :present => true, :regexp => %r{regexp}
|
521
|
+
end
|
522
|
+
#...
|
523
|
+
end
|
524
|
+
```
|
525
|
+
|
459
526
|
#### int
|
527
|
+
|
528
|
+
```ruby
|
529
|
+
post '/entry' do
|
530
|
+
form do
|
531
|
+
field :field01, :present => true, :int => true
|
532
|
+
end
|
533
|
+
#...
|
534
|
+
end
|
535
|
+
```
|
536
|
+
|
537
|
+
Fore more detailed constraint,
|
538
|
+
You can use following options as a hash.
|
539
|
+
|
540
|
+
* gt: This means >
|
541
|
+
* gte: This means >=
|
542
|
+
* lt: This means <
|
543
|
+
* lte: This means <=
|
544
|
+
* between: Or you can set Range object
|
545
|
+
|
546
|
+
```ruby
|
547
|
+
post '/entry' do
|
548
|
+
form do
|
549
|
+
field :field01, :present => true, :int => { :gt => 5, :lt => 10 }
|
550
|
+
end
|
551
|
+
#...
|
552
|
+
end
|
553
|
+
```
|
554
|
+
|
555
|
+
```ruby
|
556
|
+
post '/entry' do
|
557
|
+
form do
|
558
|
+
field :field01, :present => true, :int => { :gte => 5, :lte => 10 }
|
559
|
+
end
|
560
|
+
#...
|
561
|
+
end
|
562
|
+
```
|
563
|
+
|
564
|
+
```ruby
|
565
|
+
post '/entry' do
|
566
|
+
form do
|
567
|
+
field :field01, :present => true, :int => { :between => 5..10 }
|
568
|
+
end
|
569
|
+
#...
|
570
|
+
end
|
571
|
+
```
|
572
|
+
|
460
573
|
#### uint
|
574
|
+
|
575
|
+
Unsined integer. This doesn't allow lass than zero.
|
576
|
+
Except for that, it behaves same as integer
|
577
|
+
|
578
|
+
```ruby
|
579
|
+
post '/entry' do
|
580
|
+
form do
|
581
|
+
field :field01, :present => true, :uint => { :between => 5..10 }
|
582
|
+
end
|
583
|
+
#...
|
584
|
+
end
|
585
|
+
```
|
586
|
+
|
461
587
|
#### alpha
|
588
|
+
|
589
|
+
Alphabet
|
590
|
+
|
462
591
|
#### alpha_space
|
592
|
+
|
593
|
+
Alphabet and Space
|
594
|
+
|
463
595
|
#### alnum
|
596
|
+
|
597
|
+
Alphabet and Number
|
598
|
+
|
464
599
|
#### alnum_space
|
600
|
+
|
601
|
+
Alphabet, Number and Space
|
602
|
+
|
603
|
+
#### email
|
604
|
+
|
605
|
+
Email-Address
|
606
|
+
|
607
|
+
```ruby
|
608
|
+
post '/entry' do
|
609
|
+
form do
|
610
|
+
field :your_address, :present => true, :email => true, :bytesize => 10..255
|
611
|
+
end
|
612
|
+
#...
|
613
|
+
end
|
614
|
+
```
|
615
|
+
|
465
616
|
#### uri
|
466
617
|
|
618
|
+
Limit a scheme as Array
|
619
|
+
|
620
|
+
```ruby
|
621
|
+
post '/entry' do
|
622
|
+
form do
|
623
|
+
field :your_address, :present => true, :uri => [:http, :https], :bytesize => 10..255
|
624
|
+
end
|
625
|
+
#...
|
626
|
+
end
|
627
|
+
```
|
628
|
+
|
629
|
+
```ruby
|
630
|
+
post '/entry' do
|
631
|
+
form do
|
632
|
+
field :your_address, :present => true, :uri => [:http], :bytesize => 10..255
|
633
|
+
end
|
634
|
+
#...
|
635
|
+
end
|
636
|
+
```
|
637
|
+
|
638
|
+
If your scheme option is only one.
|
639
|
+
You can set as a String.
|
640
|
+
|
641
|
+
```ruby
|
642
|
+
post '/entry' do
|
643
|
+
form do
|
644
|
+
field :your_address, :present => true, :uri => :http, :bytesize => 10..255
|
645
|
+
end
|
646
|
+
#...
|
647
|
+
end
|
648
|
+
```
|
649
|
+
|
467
650
|
### 10: Preset Combination Constraints
|
468
651
|
|
469
652
|
#### same
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-formkeeper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-25 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Sinatra extension which handles stuff around HTML forms
|
15
15
|
email:
|