Class: Ramaze::Helper::BananaForm::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/ramaze/helper/banana_form.rb

Overview

Banana form helper manager class

Provide methods to build a form and use the banana error handling features.

Notes

This code is based on the Ramaze::Helper::BlueForm::Form class and *is not thread safe* , so make sure you’re not using it from several execution threads.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Form

Initializes an instance of the class

Accepts following extra parameters for error summary :

  • error_summary_title : sets the main summary title, defaults to “X error(s) prohibited this form from being saved” where X is the number of errors

  • error_summary_subtitle : sets the subtitle of the summary title, default to ‘Following fields reported errors:’

  • error_summary_class : sets the summary div class, defaults to ‘form_error_summary’

  • error_summary_id : sets the summary div id, defaults to ‘form_error_summary’



181
182
183
184
185
186
187
188
189
190
# File 'lib/ext/ramaze/helper/banana_form.rb', line 181

def initialize(options)
  @form_args = options.dup
  @error_encart_args = {}
  @error_encart_args[:title] =  @form_args.delete(:error_summary_title)
  @error_encart_args[:subtitle] = @form_args[:error_summary_subtitle] ? @form_args.delete(:error_summary_subtitle) : 'Following fields reported errors:'
  @error_encart_args[:class] = @form_args[:error_summary_class] ? @form_args.delete(:error_summary_class) : 'form_error_summary'
  @error_encart_args[:id] = @form_args[:error_summary_id] ? @form_args.delete(:error_summary_id) : 'form_error_summary'
  @g = Gestalt.new
  @error_encart = nil
end

Instance Attribute Details

#error_summary_argsObject (readonly)

Structure containing the summary parameters before final construction



166
167
168
# File 'lib/ext/ramaze/helper/banana_form.rb', line 166

def error_summary_args
  @error_summary_args
end

#gObject (readonly)

Gestalt HTML Builder



164
165
166
# File 'lib/ext/ramaze/helper/banana_form.rb', line 164

def g
  @g
end

Instance Method Details

#build(form_errors = {}) ⇒ Object

Builds the form

Builds the form (and summary), sets up the errors from module error holder

Called by form method, should not be invoked from outside.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/ext/ramaze/helper/banana_form.rb', line 198

def build(form_errors = {})
  @form_errors = form_errors
  @g.form(@form_args) do
    ## form validation summary
    if (  @form_errors!=nil && @error_encart_args && @form_errors.size>0)
      @error_encart_args[:title] = "#{@form_errors.size} error(s) prohibited this form from being saved" unless @error_encart_args[:title]
      @g.div(:id=>@error_encart_args[:id],:class=>@error_encart_args[:class]) {
        @g.h2("#{@error_encart_args[:title]}")
        @g.p("#{@error_encart_args[:subtitle]}")
        @g.ul() {
          @form_errors.each_pair { |name, val| @g.li("#{name} : #{val}")  }
        }
      }
    end
    ## now yeld block instructions
    if block_given?
      @g.fieldset do
        yield self
      end
    end
  end
end

#input_checkbox(label, name, checked = false, args = {}) ⇒ Object Also known as: checkbox

Creates a checkbox input field

Creates a label and an input field of type ‘checkbox’.

Default styles :

  • input field is set to checkbox_input class unless class is provided in the args hash

  • label field is set to checkbox_label class unless class_label is provided in the args hash

Error styles :

If an error occurs then both class names will be appended with a _error ( checkbox_input will become checkbox_input_error … ).

You can override those by providing class_error and class_label_error params in the args hash.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/ext/ramaze/helper/banana_form.rb', line 328

def input_checkbox(label, name, checked = false, args={})
  id = id_for(name)
  args= args.merge(:type => :checkbox, :name => name, :id => id)
  args[:checked] = 'checked' if checked

  ## set up style classes, delete errors and clean additional params
  css_classes = get_css_classes_clean(name,args,'checkbox_input','checkbox_label')
  args[:class] = css_classes[:input_class]

  @g.p do
    label_for(id, label, name, :class=>css_classes[:label_class])
    @g.input(args)
  end
end

#input_file(label, name, args = {}) ⇒ Object Also known as: file

Creates a file input field

Creates a label and an input field of type ‘checkbox’.

Default styles :

  • input field is set to file_input class unless class is provided in the args hash

  • label field is set to file_label class unless class_label is provided in the args hash

Error styles :

If an error occurs then both class names will be appended with a _error

You can override those by providing class_error and class_label_error params in the args hash.



405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/ext/ramaze/helper/banana_form.rb', line 405

def input_file(label, name, args={})
  id = id_for(name)
  args = args.merge(:type => :file, :name => name, :id => id)

  ## set up style classes, delete errors and clean additional params
  css_classes = get_css_classes_clean(name,args,'file_input','file_label')
  args[:class] = css_classes[:input_class]

  @g.p do
    label_for(id, label, name,:class=>css_classes[:label_class])
    @g.input(args)
  end
end

#input_hidden(name, value = nil) ⇒ Object Also known as: hidden

Creates a hidden input field

Creates a label and an input field of type ‘hidden’, no styling applies



424
425
426
427
428
429
# File 'lib/ext/ramaze/helper/banana_form.rb', line 424

def input_hidden(name, value = nil)
  args = {:type => :hidden, :name => name}
  args[:value] = value.to_s unless value.nil?

  @g.input(args)
end

#input_password(label, name, args = {}) ⇒ Object Also known as: password

Creates a password input field

Creates a label and an input field of type ‘password’.

Default styles :

  • input field is set to text_input class unless class is provided in the args hash

  • label field is set to text_label class unless class_label is provided in the args hash

Error styles :

If an error occurs then both class names will be appended with a _error ( text_input will become text_input_error … ).

You can override those by providing class_error and class_label_error params in the args hash.



279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/ext/ramaze/helper/banana_form.rb', line 279

def input_password(label, name, args={})
  id = id_for(name)
  args = args.merge(:type => :password, :name => name, :id => id)
  ## set up style classes, delete errors and clean additional params
  css_classes = get_css_classes_clean(name,args)
  args[:class] = css_classes[:input_class]
       
  @g.p do
    label_for(id, label, name,:class=>css_classes[:label_class])
    @g.input(args)
  end
end

#input_radio(label, name, values, options = {}) ⇒ Object Also known as: radio

Creates a group of radio buttons

Creates a label and an input field of type ‘checkbox’.

Default styles :

  • input field is set to radio_input class unless class is provided in the args hash

  • label field is set to radio_label class unless class_label is provided in the args hash

Error styles :

At the difference of other methods, only the class of the checked input field will change in case of errors.



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/ext/ramaze/helper/banana_form.rb', line 359

def input_radio(label, name, values, options = {})
  has_checked, checked = options.key?(:checked), options[:checked]

  ## not using the usual css error formatter for radio 
  input_class = options[:class] ? options[:class] : 'radio_input'
  label_class = options[:class_label] ? options[:class_label] : 'radio_label'
  error = @form_errors.delete(name.to_s)

  @g.p do
    values.each_with_index do |(value, o_name), index|
      o_name ||= value
      id = id_for("#{name}-#{index}")

      o_args = {:type => :radio, :value => value, :id => id, :name => name}
      o_args[:checked] = 'checked' if has_checked && value == checked

      clazz_elem = (error && has_checked && value == checked ) ? "#{input_class}_error" : input_class
      all_args = o_args.clone
      all_args[:class] = clazz_elem

      @g.label(:for=>id,:class=>label_class) {
        @g.input(all_args)
        @g.out << o_name
      }
    end
  end
end

#input_submit(value = nil, args = {}) ⇒ Object Also known as: submit

Creates a submit button

Creates a submit button

Default styles :

  • buton is set to button_submit class unless class is provided in the args hash



301
302
303
304
305
306
307
308
# File 'lib/ext/ramaze/helper/banana_form.rb', line 301

def input_submit(value = nil, args={})
  args = args.merge(:type => :submit)
  args[:class] = 'button_submit' unless args[:class]
  args[:value] = value unless value.nil?
  @g.p do
    @g.input(args)
  end
end

#input_text(label, name, value = nil, args = {}) ⇒ Object Also known as: text

Creates a text input field

Creates a label and an input field of type ‘text’.

Default styles :

  • input field is set to text_input class unless class is provided in the args hash

  • label field is set to text_label class unless class_label is provided in the args hash

Error styles :

If an error occurs then both class names will be appended with a _error ( text_input will become text_input_error … ).

You can override those by providing class_error and class_label_error params in the args hash.



248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/ext/ramaze/helper/banana_form.rb', line 248

def input_text(label, name, value = nil, args = {})
  id = id_for(name)
  args = args.merge(:type => :text, :name => name, :id => id)
  args[:value] = value unless value.nil?
  ## set up style classes, delete errors and clean additional params
  css_classes = get_css_classes_clean(name,args)
  args[:class] = css_classes[:input_class]
  @g.p do
    label_for(id, label, name, :class=>css_classes[:label_class])
    @g.input(args)
  end
end

#legend(text) ⇒ Object

Places a legend over the form fieldset

Params:

text : legend to display



227
228
229
# File 'lib/ext/ramaze/helper/banana_form.rb', line 227

def legend(text)
  @g.legend(text)
end

#select(label, name, values, options = {}) ⇒ Object

Creates a selection group input

Creates the labels and an input field for a group selection

Default styles :

  • input field is set to select_input class unless class is provided in the args hash

  • label field is set to select_label class unless class_label is provided in the args hash

Error styles :

If an error occurs then both class names will be appended with a _error

You can override those by providing class_error and class_label_error params in the args hash.



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/ext/ramaze/helper/banana_form.rb', line 478

def select(label, name, values, options = {})
  id = id_for(name)
  multiple, size = options.values_at(:multiple, :size)

  args = {:id => id}
  args[:multiple] = 'multiple' if multiple
  args[:size] = (size || multiple || 1).to_i
  args[:name] = multiple ? "#{name}[]" : name
  has_selected, selected = options.key?(:selected), options[:selected]
  css_classes = get_css_classes_clean(name,options,'select_input','select_label')
  @g.p do
    label_for(id, label, name,:class=>css_classes[:label_class])
    @g.select args do
      values.each do |value, o_name|
        o_name ||= value
        o_args = {:value => value}
        o_args[:selected] = 'selected' if has_selected && value == selected
        o_args[:class] = css_classes[:input_class]
        @g.option(o_args){ o_name }
      end
    end
  end
end

#textarea(label, name, value = nil, options = {}) ⇒ Object

Creates a text area input field

Creates a label and an input field of type text area.

Default styles :

  • input field is set to area_input class unless class is provided in the args hash

  • label field is set to area_label class unless class_label is provided in the args hash

Error styles :

If an error occurs then both class names will be appended with a _error

You can override those by providing class_error and class_label_error params in the args hash.



448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/ext/ramaze/helper/banana_form.rb', line 448

def textarea(label, name, value = nil, options={})
  id = id_for(name)
  args = {:name => name, :id => id }

  css_classes = get_css_classes_clean(name,options,'area_input','area_label')
  args[:class] = css_classes[:input_class]
  
  @g.p do
    label_for(id, label, name,:class=>css_classes[:label_class])
    @g.textarea(args){ value }
  end
end

#to_sObject

Converts to string

Returns the string containing the HTML of the form



506
507
508
# File 'lib/ext/ramaze/helper/banana_form.rb', line 506

def to_s
  @g.to_s
end