formeze 1.9.0 → 1.9.1
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.
- data/README.md +23 -21
- data/formeze.gemspec +1 -1
- data/lib/formeze.rb +19 -3
- data/spec/formeze_spec.rb +35 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -5,7 +5,7 @@ Formeze: A little library for handling form data/input
|
|
5
5
|
Motivation
|
6
6
|
----------
|
7
7
|
|
8
|
-
Most web apps built for end users will need to process
|
8
|
+
Most web apps built for end users will need to process url-encoded form data.
|
9
9
|
Registration forms, profile forms, checkout forms, contact forms, and forms
|
10
10
|
for adding/editing application specific data. As developers we would like to
|
11
11
|
process this data safely, to minimise the possibility of security holes
|
@@ -25,8 +25,7 @@ $ gem install formeze
|
|
25
25
|
Example usage
|
26
26
|
-------------
|
27
27
|
|
28
|
-
|
29
|
-
minimal example, which defines a form with a single "title" field:
|
28
|
+
Here is a minimal example, which defines a form with a single field:
|
30
29
|
|
31
30
|
```ruby
|
32
31
|
class ExampleForm < Formeze::Form
|
@@ -68,8 +67,8 @@ normal running of your application), and key/value errors (which most likely
|
|
68
67
|
indicate either developer error, or form tampering).
|
69
68
|
|
70
69
|
For the latter case, the `parse` method that formeze provides will raise a
|
71
|
-
Formeze::KeyError or a Formeze::ValueError exception if the structure of
|
72
|
-
form data does not match the field definitions.
|
70
|
+
`Formeze::KeyError` or a `Formeze::ValueError` exception if the structure of
|
71
|
+
the form data does not match the field definitions.
|
73
72
|
|
74
73
|
After calling `parse` you can check that the form is valid by calling the
|
75
74
|
`#valid?` method. If it isn't you can call the `errors` method which will
|
@@ -87,15 +86,18 @@ and they cannot contain newlines. These restrictions can be overridden
|
|
87
86
|
by setting various field options.
|
88
87
|
|
89
88
|
Defining a field without any options works well for a simple text input.
|
90
|
-
If the default
|
91
|
-
|
89
|
+
If the default length limit is too big or too small you can override it
|
90
|
+
by setting the `maxlength` option. For example:
|
92
91
|
|
93
92
|
```ruby
|
94
|
-
field :title,
|
93
|
+
field :title, maxlength: 200
|
95
94
|
```
|
96
95
|
|
97
|
-
|
98
|
-
|
96
|
+
Similarly there is a `minlength` option for validating fields that should
|
97
|
+
have a minimum number of characters (e.g. passwords).
|
98
|
+
|
99
|
+
Fields are required by default. Specify the `required` option if the field
|
100
|
+
is not required, i.e. the value of the field can be blank/empty. For example:
|
99
101
|
|
100
102
|
```ruby
|
101
103
|
field :title, required: false
|
@@ -113,7 +115,7 @@ If you are dealing with textareas (i.e. multiple lines of text) then you can
|
|
113
115
|
set the `multiline` option to allow newlines. For example:
|
114
116
|
|
115
117
|
```ruby
|
116
|
-
field :description,
|
118
|
+
field :description, maxlength: 500, multiline: true
|
117
119
|
```
|
118
120
|
|
119
121
|
Error messages will include the field label, which by default is set to the
|
@@ -131,7 +133,7 @@ well defined formats, like numbers. For example:
|
|
131
133
|
```ruby
|
132
134
|
field :number, pattern: /\A[1-9]\d*\z/
|
133
135
|
|
134
|
-
field :card_security_code,
|
136
|
+
field :card_security_code, maxlength: 5, pattern: /\A\d+\z/
|
135
137
|
```
|
136
138
|
|
137
139
|
If you want to validate that the field value belongs to a set of predefined
|
@@ -157,8 +159,9 @@ allow multiple values. For example:
|
|
157
159
|
field :colour, multiple: true, values: Colour.keys
|
158
160
|
```
|
159
161
|
|
160
|
-
|
161
|
-
to this field will return an array of strings instead
|
162
|
+
Note that unlike all the other examples so far, reading the attribute
|
163
|
+
that corresponds to this field will return an array of strings instead
|
164
|
+
of a single string.
|
162
165
|
|
163
166
|
Sometimes you'll only want the field to be defined if some condition is true.
|
164
167
|
The condition may depend on the state of other form fields, or some external
|
@@ -191,8 +194,8 @@ and validated if the `same_address` checkbox is checked.
|
|
191
194
|
Validation errors can be a frustrating experience for end users, so ideally
|
192
195
|
we want to [be liberal in what we accept](http://en.wikipedia.org/wiki/Jon_Postel#Postel.27s_Law),
|
193
196
|
but at the same time ensuring that data is consistently formatted to make it
|
194
|
-
easy for us to process.
|
195
|
-
|
197
|
+
easy for us to process. The `scrub` option can be used to specify methods for
|
198
|
+
"cleaning" input data before validation. For example:
|
196
199
|
|
197
200
|
```ruby
|
198
201
|
field :postcode, scrub: [:strip, :squeeze, :upcase]
|
@@ -200,15 +203,14 @@ field :postcode, scrub: [:strip, :squeeze, :upcase]
|
|
200
203
|
|
201
204
|
The input for this field will have leading/trailing whitespace stripped,
|
202
205
|
double (or more) spaces squeezed, and the result upcased automatically.
|
203
|
-
|
204
|
-
|
205
|
-
the `Formeze.scrub_methods` hash.
|
206
|
+
Custom scrub methods can be defined by adding a symbol/proc entry to the
|
207
|
+
`Formeze.scrub_methods` hash.
|
206
208
|
|
207
209
|
|
208
210
|
Rails usage
|
209
211
|
-----------
|
210
212
|
|
211
|
-
This is the basic pattern for using a formeze form in a
|
213
|
+
This is the basic pattern for using a formeze form in a Rails controller:
|
212
214
|
|
213
215
|
```ruby
|
214
216
|
form = SomeForm.new
|
@@ -222,7 +224,7 @@ end
|
|
222
224
|
```
|
223
225
|
|
224
226
|
Formeze will automatically ignore the "utf8" and "authenticity_token"
|
225
|
-
parameters that Rails uses
|
227
|
+
parameters that Rails uses.
|
226
228
|
|
227
229
|
|
228
230
|
Sinatra usage
|
data/formeze.gemspec
CHANGED
data/lib/formeze.rb
CHANGED
@@ -30,6 +30,8 @@ module Formeze
|
|
30
30
|
|
31
31
|
yield error(:too_long, 'is too long') if too_long?(value)
|
32
32
|
|
33
|
+
yield error(:too_short, 'is too short') if too_short?(value)
|
34
|
+
|
33
35
|
yield error(:no_match, 'is invalid') if no_match?(value)
|
34
36
|
|
35
37
|
yield error(:bad_value, 'is invalid') if values? && !values.include?(value)
|
@@ -70,8 +72,20 @@ module Formeze
|
|
70
72
|
too_many_characters?(value) || too_many_words?(value)
|
71
73
|
end
|
72
74
|
|
75
|
+
def too_short?(value)
|
76
|
+
@options.has_key?(:minlength) && value.chars.count < @options.fetch(:minlength)
|
77
|
+
end
|
78
|
+
|
73
79
|
def too_many_characters?(value)
|
74
|
-
|
80
|
+
if @options.has_key?(:maxlength)
|
81
|
+
value.chars.count > @options.fetch(:maxlength)
|
82
|
+
elsif @options.has_key?(:char_limit)
|
83
|
+
Kernel.warn '[formeze] :char_limit option is deprecated, please use :maxlength instead'
|
84
|
+
|
85
|
+
value.chars.count > @options.fetch(:char_limit)
|
86
|
+
else
|
87
|
+
value.chars.count > 64
|
88
|
+
end
|
75
89
|
end
|
76
90
|
|
77
91
|
def too_many_words?(value)
|
@@ -126,7 +140,9 @@ module Formeze
|
|
126
140
|
module ArrayAttrAccessor
|
127
141
|
def array_attr_reader(name)
|
128
142
|
define_method(name) do
|
129
|
-
|
143
|
+
ivar = :"@#{name}"
|
144
|
+
|
145
|
+
instance_variable_defined?(ivar) ? Array(instance_variable_get(ivar)) : []
|
130
146
|
end
|
131
147
|
end
|
132
148
|
|
@@ -134,7 +150,7 @@ module Formeze
|
|
134
150
|
define_method(:"#{name}=") do |value|
|
135
151
|
ivar = :"@#{name}"
|
136
152
|
|
137
|
-
instance_variable_set(ivar,
|
153
|
+
instance_variable_set(ivar, send(name) + [value])
|
138
154
|
end
|
139
155
|
end
|
140
156
|
|
data/spec/formeze_spec.rb
CHANGED
@@ -244,6 +244,40 @@ describe 'FormWithCharacterLimitedField after parsing input with too many charac
|
|
244
244
|
end
|
245
245
|
end
|
246
246
|
|
247
|
+
class FormWithMaxLengthField < Formeze::Form
|
248
|
+
field :title, :maxlength => 16
|
249
|
+
end
|
250
|
+
|
251
|
+
describe 'FormWithMaxLengthField after parsing input with too many characters' do
|
252
|
+
before do
|
253
|
+
@form = FormWithMaxLengthField.new
|
254
|
+
@form.parse('title=This+Title+Will+Be+Too+Long')
|
255
|
+
end
|
256
|
+
|
257
|
+
describe 'valid query method' do
|
258
|
+
it 'returns false' do
|
259
|
+
@form.valid?.must_equal(false)
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
class FormWithMinLengthField < Formeze::Form
|
265
|
+
field :title, :minlength => 8
|
266
|
+
end
|
267
|
+
|
268
|
+
describe 'FormWithMinLengthField after parsing input with too few characters' do
|
269
|
+
before do
|
270
|
+
@form = FormWithMinLengthField.new
|
271
|
+
@form.parse('title=Hello')
|
272
|
+
end
|
273
|
+
|
274
|
+
describe 'valid query method' do
|
275
|
+
it 'returns false' do
|
276
|
+
@form.valid?.must_equal(false)
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
247
281
|
class FormWithWordLimitedField < Formeze::Form
|
248
282
|
field :title, :word_limit => 2
|
249
283
|
end
|
@@ -611,7 +645,7 @@ describe 'FormClassWithExplicitSetupCall' do
|
|
611
645
|
end
|
612
646
|
|
613
647
|
it 'includes the formeze class methods and instance methods' do
|
614
|
-
singleton_class = if @
|
648
|
+
singleton_class = if @form_class.respond_to?(:singleton_class)
|
615
649
|
@form_class.singleton_class
|
616
650
|
else
|
617
651
|
(class << @form_class; self; end)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formeze
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.9.
|
4
|
+
version: 1.9.1
|
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:
|
12
|
+
date: 2013-01-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|