Module: RatPack::Forms

Defined in:
lib/ratpack/forms.rb

Instance Method Summary collapse

Instance Method Details

#button(contents, attrs = {}) ⇒ Object



24
25
26
# File 'lib/ratpack/forms.rb', line 24

def button(contents, attrs = {})
  form_field(:button, contents, attrs)
end

#check_box(attrs) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ratpack/forms.rb', line 40

def check_box(attrs)
  html = ""
  label = build_field(attrs)
  attrs[:checked] = "checked" if attrs[:checked]
  if attrs.delete(:boolean)
    on, off = attrs.delete(:on), attrs.delete(:off)
    html << hidden_field(:name => attrs[:name], :value => off)
    html << self_closing_tag(:input, {:type => "checkbox", :value => on}.merge(attrs))
  else
    closed_form_field(:input, {:type => "checkbox"}.merge(attrs))
    html << self_closing_tag(:input, {:type => "checkbox"}.merge(attrs))
  end
  html + label
end

#error_messages_for(obj = nil, opts = {}) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/ratpack/forms.rb', line 3

def error_messages_for(obj = nil, opts = {})
  return unless obj.respond_to?(:errors)
  return if obj.errors.blank?
  html = tag(:h2, "Form submission failed because of #{obj.errors.size} #{pluralize("error",obj.errors.size)}")
  msgs = obj.errors.map{|error| error.map{|msg| tag(:li,msg)}}.flatten
  tag(:div, html + tag(:ul,msgs), :class => "error")
end

#radio_button(attrs) ⇒ Object



28
29
30
# File 'lib/ratpack/forms.rb', line 28

def radio_button(attrs)
  closed_form_field(:input, {:type => "radio"}.merge(attrs))
end

#radio_group(arr, attrs = {}) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/ratpack/forms.rb', line 32

def radio_group(arr, attrs = {})
  arr.map do |ind_attrs|
    ind_attrs = {:value => ind_attrs} unless ind_attrs.is_a?(Hash)
    joined = attrs.merge(ind_attrs)
    radio_button(joined)
  end.join
end

#reset(value = "Reset", attrs = {}) ⇒ Object



67
68
69
70
71
72
# File 'lib/ratpack/forms.rb', line 67

def reset(value="Reset", attrs = {})
  attrs[:type]  ||= "reset"
  attrs[:name]  ||= "reset"
  attrs[:value] ||= value
  closed_form_field(:input, attrs)
end

#select(attrs = {}) ⇒ Object



55
56
57
58
# File 'lib/ratpack/forms.rb', line 55

def select(attrs = {})
  attrs[:name] << "[]" if attrs[:multiple] && !(attrs[:name] =~ /\[\]$/)
  form_field(:select, options_for(attrs), attrs)
end

#submit(value = "Submit", attrs = {}) ⇒ Object



60
61
62
63
64
65
# File 'lib/ratpack/forms.rb', line 60

def submit(value="Submit", attrs = {})
  attrs[:type]  ||= "submit"
  attrs[:name]  ||= "submit"
  attrs[:value] ||= value
  closed_form_field(:input, attrs)
end

#text_area(attrs = {}) ⇒ Object



20
21
22
# File 'lib/ratpack/forms.rb', line 20

def text_area(attrs = {})
  form_field(:textarea, attrs.delete(:value) || "", attrs)
end