Sha256: 34460f5d5424738d885b9625b993204f53a4d37fff6e6a5996fdd81ffc3fa759

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

require 'monkey_forms'

class OrderForm
  include MonkeyForms::Form
  attr_reader :an_attribute

  # Declares a few attributes on the form.
  form_attributes :name, :email, :city, :state, :line_items, :upload
  custom_attributes :user_id
  form_name :cart

  # This form serializes the submit into a gzip'd cookie with a name
  # of 'order_cookie'.
  set_form_storage(
    MonkeyForms::Serializers::GzipCookie.new(
      :name => 'order_cookie',
      :domain => 'test.domain.com',
      :secure => true,
      :httponly => true))

  after_initialize :set_attribute, :set_default_state

  # We must submit an email address for the form to validate.
  validates :email, :presence => true

  set_form_attribute_human_names :name => "Your Name"

  validation_group :cart do
    # Scope some of the validation checks
    validates :name, :presence => true
  end

  validation_group :address do
    validates :city,  :presence => true
    validates :state, :presence => true
  end

  # This is a method that uses some form attributes.
  def person
    "#{ name } <#{ email }>"
  end

  private

  def set_attribute
    @an_attribute = true
  end

  def set_default_state
    if state.blank?
      self.state = "WA"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
monkey_forms-0.0.20 test/forms/order_form.rb
monkey_forms-0.0.19 test/forms/order_form.rb