kiss 1.7.4 → 1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,239 +0,0 @@
1
- # Kiss Form Field types.
2
-
3
- class Kiss
4
- class Form
5
- class HiddenField < Field; end
6
- class TextField < Field; end
7
-
8
- class TextAreaField < Field
9
- _attr_accessor :rows, :cols
10
-
11
- def initialize(*args)
12
- @_rows = 5
13
- @_cols = 20
14
- super(*args)
15
- end
16
-
17
- def element_html(attrs = {})
18
- content_tag_html(
19
- 'textarea',
20
- value_string,
21
- attrs.merge(
22
- :rows => @_rows ||= 1,
23
- :cols => @_cols ||= 1
24
- )
25
- ) + tip_html(attrs)
26
- end
27
- end
28
-
29
- class PasswordField < Field
30
- def element_html(*args)
31
- input_tag_html(*args)
32
- end
33
- end
34
-
35
- class FileField < Field
36
- def element_html(attrs = {})
37
- input_tag_html(attrs) + tip_html(attrs)
38
- end
39
-
40
- def get_file_name; end
41
-
42
- def get_file_data; end
43
-
44
- def require_value(enter_verb)
45
- p = param
46
- return add_error("Please choose #{label}") unless p && p[:type]
47
- end
48
-
49
- def validate
50
- require_value(nil) if @_required
51
- end
52
- end
53
-
54
- class SubmitField < Field
55
- def initialize(*args)
56
- @_save = false
57
- super(*args)
58
- end
59
-
60
- def element_html(*args)
61
- elements_html(*args).join(' ')
62
- end
63
-
64
- def elements_html(attrs = {})
65
- @_options.map do |option|
66
- input_tag_html(attrs.merge( :value => value_to_s(option) ))
67
- end
68
- end
69
- end
70
-
71
- # ------ MultiChoiceField
72
-
73
- class MultiChoiceField < Field
74
- def initialize(*args, &block)
75
- @_options_display_transform = :to_s
76
-
77
- super(*args, &block)
78
- end
79
-
80
- def option_pairs
81
- pairs = if @_options_value_key
82
- if @_options_display_key.is_a?(Proc)
83
- @_options.map {|option| [ option[@_options_value_key], @_options_display_key.call(option) ]}
84
- else
85
- @_options.map {|option| [
86
- option[@_options_value_key] || option.send(@_options_value_key),
87
- option[@_options_display_key] || option.send(@_options_display_key)
88
- ]}
89
- end
90
- else
91
- @_display_format = @_format
92
- @_options.map {|option| [ option, option ]}
93
- end
94
-
95
- pairs
96
- end
97
-
98
- def has_option_value?(v)
99
- !(@_options_value_key ?
100
- @_options.select {|o| value_to_s(o[@_options_value_key]) == v } :
101
- @_options.select {|o| value_to_s(o) == v }
102
- ).empty?
103
- end
104
-
105
- def validate
106
- if @_other && param == 'other'
107
- @_param = @_form.params[@_name+'.other']
108
- end
109
- super('select')
110
-
111
- if @_value =~ /\S/ && !has_option_value?(@_value)
112
- add_error "Invalid selection"
113
- end
114
- end
115
- end
116
-
117
- class SelectField < MultiChoiceField
118
- def element_html(attrs = {})
119
- return 'No options' unless @_options.size > 0
120
-
121
- @_choose_here ||= 'Choose Here'
122
- placeholder_html = %Q(<option value="">#{@_choose_here}</option>)
123
-
124
- options_html = option_pairs.map do |option_value, option_display|
125
- option_value_string = value_to_s(option_value)
126
- selected = (value_string == option_value_string) ? ' selected' : ''
127
- %Q(<option value="#{option_value_string}"#{selected}>#{display_to_s(option_display)}</option>)
128
- end.join
129
-
130
- content_tag_html('select', placeholder_html + options_html, attrs) + other_field_html + tip_html(attrs)
131
- end
132
- end
133
-
134
- class RadioField < MultiChoiceField
135
- def element_html(attrs = {})
136
- column_layout(elements_html(attrs)) + other_field_html + tip_html(attrs)
137
- end
138
-
139
- def elements_html(attrs = {})
140
- option_pairs.map do |option_value, option_display|
141
- option_value_string = value_to_s(option_value)
142
- input_tag_html(
143
- attrs.merge( :type => 'radio', :value => option_value_string ),
144
- (value_string == option_value_string) ? 'checked' : ''
145
- ) + @_currency.to_s + display_to_s(option_display)
146
- end
147
- end
148
- end
149
-
150
- class BooleanField < RadioField
151
- def initialize(*args, &block)
152
- @_options = [[1, 'Yes'], [0, 'No']]
153
- super(*args, &block)
154
- end
155
- end
156
-
157
-
158
- # ------ MultiValueField
159
-
160
- class MultiValueField < MultiChoiceField
161
- def param
162
- @_form.params[@_name.to_s+'[]'] || []
163
- end
164
-
165
- def validate
166
- begin
167
- @_value = param.map { |p| @_format.validate(p) }
168
- rescue Kiss::Format::ValidateError => e
169
- return add_error("#{e.message.capitalize}")
170
- end
171
-
172
- if @_value.empty? && @_required
173
- return add_error "Please select at least one #{@_label.downcase.singularize}"
174
- end
175
-
176
- if @_min_value_size && @_value.size < @_min_value_size
177
- return add_error "Please select at least #{@_min_value_size.of(@_label.downcase)}"
178
- end
179
-
180
- if @_max_value_size && @_value.size > @_max_value_size
181
- return add_error "Please select no more than #{@_max_value_size.of(@_label.downcase)}"
182
- end
183
-
184
- @_value.each do |v|
185
- unless has_option_value?(v)
186
- return add_error "Invalid selection"
187
- end
188
- end
189
- end
190
-
191
- def selected_option_values
192
- @_selected_option_values ||= @_value ? Hash[ *(@_value.map {|v| [value_to_s(v), true]}.flatten) ] : {}
193
- end
194
- end
195
-
196
- class CheckboxField < MultiValueField
197
- def element_html(attrs = {})
198
- hidden_options = @_hidden_join ? input_tag_html(
199
- :type => 'hidden',
200
- :name => "#{@_name}_options",
201
- :value => option_pairs.map {|option_value, option_display| value_to_s(option_value) }.join(@_hidden_join)
202
- ) : ''
203
-
204
- column_layout(elements_html(attrs)) + other_field_html + hidden_options + tip_html(attrs)
205
- end
206
-
207
- def elements_html(attrs = {})
208
- name = @_name.to_s+'[]'
209
- option_pairs.map do |option_value, option_display|
210
- option_value_string = value_to_s(option_value)
211
-
212
- input_tag_html(
213
- attrs.merge( :name => name, :value => option_value_string ),
214
- selected_option_values[option_value_string] ? 'checked' : ''
215
- ) + @_currency.to_s + display_to_s(option_display)
216
- end
217
- end
218
- end
219
-
220
- class MultiSelectField < MultiValueField
221
- def element_html(attrs = {})
222
- options_html = option_pairs.map do |option_value, option_display|
223
- option_value_string = value_to_s(option_value)
224
- selected = selected_option_values[option_value_string] ? ' selected' : ''
225
- %Q(<option value="#{option_value_string}"#{selected}>#{display_to_s(option_display)}</option>)
226
- end.join
227
-
228
- content_tag_html(
229
- 'select',
230
- options_html,
231
- attrs,
232
- 'multiple'
233
- )
234
- end
235
- end
236
-
237
- # not implemented yet: MultiTextField,
238
- end
239
- end