Class: Dima::Html::Form

Inherits:
AbstractForm show all
Defined in:
lib/dima/html/form.rb

Overview

Form.

Instance Attribute Summary collapse

Attributes inherited from AbstractForm

#collapsed, #help, #icon, #method, #submit, #title

Instance Method Summary collapse

Methods inherited from AbstractForm

#actions, #html, #to_n

Methods included from Init

#initialize

Methods inherited from Element

#id, #id=

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



97
98
99
# File 'lib/dima/html/form.rb', line 97

def auth_token
  @auth_token
end

#editObject

Returns the value of attribute edit.



95
96
97
# File 'lib/dima/html/form.rb', line 95

def edit
  @edit
end

#error_fieldsObject

Returns the value of attribute error_fields.



96
97
98
# File 'lib/dima/html/form.rb', line 96

def error_fields
  @error_fields
end

Instance Method Details

#<<(v) ⇒ Object



123
124
125
# File 'lib/dima/html/form.rb', line 123

def << (v)
  self.val = v  
end

#>>(v) ⇒ Object



127
128
129
130
# File 'lib/dima/html/form.rb', line 127

def >> (v)
  col1.fields.each { |f| f >> v unless f.readonly }
  col2.fields.each { |f| f >> v unless f.readonly }
end

#col1Object

First column.



133
134
135
# File 'lib/dima/html/form.rb', line 133

def col1
  @col1 ||= FormFields.new
end

#col2Object

Second column.



138
139
140
# File 'lib/dima/html/form.rb', line 138

def col2
  @col2 ||= FormFields.new
end

#generate_contentObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/dima/html/form.rb', line 142

def generate_content
  if edit
    form = Node.new(tag: 'form', attributes: { method: (self.method || 'post'), 'accept-charset' => 'UTF-8' })
    if self.auth_token
      flds = Node.new(tag: 'div', attributes: { style: 'padding: 0; margin: 0; height: 0; width: 0; display: inline;' })
      flds << Node.new(tag: 'input', attributes: { name: 'authenticity_token', type: 'hidden', value: self.auth_token } )
      form << flds
    end
  else
    form = Node.new(tag: 'div')
  end
  form << generate_cols
  form << generate_submit if edit
  form
end

#update(params) ⇒ Object

Update with request parameters.



159
160
161
162
# File 'lib/dima/html/form.rb', line 159

def update(params)
  col1.fields.each { |f| f >> @val }
  col2.fields.each { |f| f >> @val }
end

#valObject



119
120
121
# File 'lib/dima/html/form.rb', line 119

def val
  @val
end

#val=(v) ⇒ Object

assign model value for this form



113
114
115
116
117
# File 'lib/dima/html/form.rb', line 113

def val=(v)
  @val = v
  col1.fields.each { |f| f << v }
  col2.fields.each { |f| f << v }
end

#valid?Boolean

Returns:

  • (Boolean)


107
108
109
110
# File 'lib/dima/html/form.rb', line 107

def valid?
  validate
  self.error_fields.empty?
end

#validateObject



99
100
101
102
103
104
105
# File 'lib/dima/html/form.rb', line 99

def validate
  self.error_fields = []
  (col1.fields + col2.fields).each do |f|
    f.validate unless f.readonly
    self.error_fields << f if f.has_errors
  end
end