Class: HungryForm::Form
- Inherits:
-
Object
- Object
- HungryForm::Form
- Defined in:
- lib/hungryform/form.rb
Overview
HungryForm is an object that manages form creation and validation. A sample object could look like this:
form = HungryForm::Form.new :params => params do
page :about_yourself do
text_field :first_name, :required => true
text_field :last_name, :required => true
checkbox :dog, label: "Do you have a dog?"
end
page :about_your_dog, visible: '{"SET": "about_yourself_dog"}' do
text_field :name, :required
text_area :what_can_it_do, label: "What can it do?"
end
end
A form must contain only pages. Whenever a specific form error occurres inside the form it raises a HungryFormException
When a new instance of a HungryForm::Form is created, it uses attributes to build a structure of itself. The pages with dependencies, that resolve during this process will be included in the form.pages array. Pages without dependencies will be allways resolved. The rest of the pages will be ignored.
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
Instance Method Summary collapse
-
#elements ⇒ Object
Get all the elements the form consists of.
-
#initialize(attributes = {}, &block) ⇒ Form
constructor
A new instance of Form.
- #invalid? ⇒ Boolean
- #next_page ⇒ Object
-
#page(name, attributes = {}, &block) ⇒ Object
Create a form page.
- #prev_page ⇒ Object
-
#to_hash ⇒ Object
Get an entire hash of the form, including every element on every visible page.
- #to_json ⇒ Object
-
#valid? ⇒ Boolean
Entire form validation.
-
#values ⇒ Object
Create a hash of form elements values.
Constructor Details
#initialize(attributes = {}, &block) ⇒ Form
Returns a new instance of Form.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/hungryform/form.rb', line 38 def initialize(attributes = {}, &block) unless block_given? fail HungryFormException, 'No form structure block given' end @resolver = Resolver.new(attributes.slice(:params)) @pages = [] instance_eval(&block) if @resolver.params[:page] @current_page = pages.find { |p| p.name.to_s == @resolver.params[:page] } end @current_page ||= pages.first end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
36 37 38 |
# File 'lib/hungryform/form.rb', line 36 def current_page @current_page end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
36 37 38 |
# File 'lib/hungryform/form.rb', line 36 def pages @pages end |
Instance Method Details
#elements ⇒ Object
Get all the elements the form consists of
79 80 81 |
# File 'lib/hungryform/form.rb', line 79 def elements @resolver.elements end |
#invalid? ⇒ Boolean
74 75 76 |
# File 'lib/hungryform/form.rb', line 74 def invalid? !valid? end |
#next_page ⇒ Object
104 105 106 107 108 |
# File 'lib/hungryform/form.rb', line 104 def next_page pages.each_cons(2) do |page, next_page| return next_page if page == current_page end end |
#page(name, attributes = {}, &block) ⇒ Object
Create a form page
56 57 58 59 |
# File 'lib/hungryform/form.rb', line 56 def page(name, attributes = {}, &block) page = Elements::Page.new(name, nil, @resolver, attributes, &block) pages << page if page.visible? end |
#prev_page ⇒ Object
110 111 112 113 114 |
# File 'lib/hungryform/form.rb', line 110 def prev_page pages.each_cons(2) do |prev_page, page| return prev_page if page == current_page end end |
#to_hash ⇒ Object
Get an entire hash of the form, including every element on every visible page
85 86 87 |
# File 'lib/hungryform/form.rb', line 85 def to_hash { pages: pages.map(&:to_hash) } end |
#to_json ⇒ Object
89 90 91 |
# File 'lib/hungryform/form.rb', line 89 def to_json JSON.generate(to_hash) end |
#valid? ⇒ Boolean
Entire form validation. Loops through the form pages and validates each page individually.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hungryform/form.rb', line 63 def valid? is_valid = true pages.each do |page| # Loop through pages to get all errors is_valid = false if page.invalid? end is_valid end |
#values ⇒ Object
Create a hash of form elements values
94 95 96 97 98 99 100 101 102 |
# File 'lib/hungryform/form.rb', line 94 def values active_elements = elements.select do |name, el| el.is_a? Elements::Base::ActiveElement end active_elements.each_with_object({}) do |(name, el), o| o[name.to_sym] = el.value end end |