Module: Ramaze::Helper::BlueForm

### Home Classes Methods

### In Files

- lib/ramaze/helper/blue_form.rb

### Namespace

- CLASS Ramaze::Helper::BlueForm::Form

### Methods

- #form_error
- #form_errors
- #form_errors_from_model
- #form_for

### Files

- Gemfile
- Rakefile

### Class/Module Index

Quicksearch

- Ramaze
- Ramaze::App
- Ramaze::Bin
- Ramaze::Bin::Create
- Ramaze::Bin::Runner
- Ramaze::Cache
- Ramaze::Cache::LRU
- Ramaze::Cache::LocalMemCache
- Ramaze::Cache::MemCache
- Ramaze::Cache::Redis
- Ramaze::Cache::Sequel
- Ramaze::Controller
- Ramaze::CoreExtensions
- Ramaze::CoreExtensions::Object
- Ramaze::CoreExtensions::String
- Ramaze::Current
- Ramaze::DefaultController
- Ramaze::Files
- Ramaze::Gestalt
- Ramaze::Helper
- Ramaze::Helper::Auth
- Ramaze::Helper::Bench
- Ramaze::Helper::BlueForm
- Ramaze::Helper::BlueForm::Form
- Ramaze::Helper::CSRF
- Ramaze::Helper::Cache
- Ramaze::Helper::Cache::SingletonMethods
- Ramaze::Helper::Email
- Ramaze::Helper::Erector
- Ramaze::Helper::Flash
- Ramaze::Helper::Formatting
- Ramaze::Helper::Gestalt
- Ramaze::Helper::Gravatar
- Ramaze::Helper::Identity
- Ramaze::Helper::Layout
- Ramaze::Helper::Layout::SingletonMethods
- Ramaze::Helper::Link
- Ramaze::Helper::Localize
- Ramaze::Helper::Localize::Dictionary
- Ramaze::Helper::Localize::Parser
- Ramaze::Helper::Markaby
- Ramaze::Helper::Maruku
- Ramaze::Helper::Paginate
- Ramaze::Helper::Paginate::Paginator
- Ramaze::Helper::Paginate::Paginator::ArrayPager
- Ramaze::Helper::Paginate::Paginator::DataMapperPager
- Ramaze::Helper::Remarkably
- Ramaze::Helper::RequestAccessor
- Ramaze::Helper::SendFile
- Ramaze::Helper::SimpleCaptcha
- Ramaze::Helper::Stack
- Ramaze::Helper::Thread
- Ramaze::Helper::Upload
- Ramaze::Helper::Upload::ClassMethods
- Ramaze::Helper::Upload::UploadedFile
- Ramaze::Helper::UserHelper
- Ramaze::Helper::UserHelper::Wrapper
- Ramaze::Helper::XHTML
- Ramaze::LRUHash
- Ramaze::Logger
- Ramaze::Logger::Informer
- Ramaze::Logger::LogHub
- Ramaze::Logger::Logger
- Ramaze::Logger::RotatingInformer
- Ramaze::Logger::Syslog
- Ramaze::Logging
- Ramaze::Reloader
- Ramaze::Reloader::Hooks
- Ramaze::Reloader::WatchInotify
- Ramaze::Reloader::WatchStat
- Ramaze::Request
- Ramaze::Response
- Ramaze::SpecLogger
- Ramaze::View
- Ramaze::View::Erector
- Ramaze::View::Erubis
- Ramaze::View::Ezamar
- Ramaze::View::Gestalt
- Ramaze::View::Haml
- Ramaze::View::Liquid
- Ramaze::View::Liquid::Tag
- Ramaze::View::Lokar
- Ramaze::View::Mustache
- Ramaze::View::Mustache::RamazeContext
- Ramaze::View::Nagoro
- Ramaze::View::Remarkably
- Ramaze::View::Sass
- Ramaze::View::Slippers
- Ramaze::View::Tagz
- Ramaze::View::Tagz::Methods
- Ramaze::View::Tenjin
- Innate
- Innate::View
- Innate::View::Slim
- Nagoro
- Nagoro::Pipe
- Nagoro::Pipe::RenderPartial
- Bacon
- Bacon::PrettyOutput
- Erector
- Erector::Widget
- Rack
- Rack::RouteExceptions
- Tenjin
- Tenjin::Template
- BlankSlate
- Controller
- MainController
- Object

No matching classes.

# Ramaze::Helper::BlueForm

The BlueForm helper tries to be an even better way to build forms programmatically. By using a simple block you can quickly create all the required elements for your form.

See {Ramaze::Helper::BlueForm::Form} for all the available methods.

## Form Data

As stated earlier it's possible to pass an object to the form_for() method. What kind of object this is, a database result object or an OpenStruct object doesn't matter as long as the attributes can be accessed outside of the object (this can be done using attr_readers). This makes it extremely easy to directly pass a result object from your favourite ORM. Example:

    @data = User[1]

    form_for(@data, :method => :post) do |f|
      f.input_text 'Username', :username
    end

The object comes handy when you want to do server-side form validation: if the form can not be validated, just send back the object with keys containing what the user has filled. The fields will be populated with these values, so the user doesn't have to retype everything.

If you don't want to use an object you can simply set the first parameter to nil.

## HTML Output

The form helper uses Gestalt, Ramaze's custom HTML builder that works somewhat like Erector. The output is very minimalistic, elements such as legends and fieldsets have to be added manually.

If you need to add elements not covered by Form methods (e.g. `<div>` tags), you can access the form Gestalt instance with the g() method and generate your tags like this :

    form_for(@result, :method => :post) do |f|
      f.g.div(:class => "awesome") do
        ...
      end
    end

Each combination of a label and input element will be wrapped in `<p>` tags.

When using the form helper as a block in your templates it's important to remember that the result is returned and not displayed in the browser directly. When using Etanni this would result in something like the following:

    #{form_for(@result, :method => :post) do |f|
      f.input_text 'Text label', :textname, 'Chunky bacon!'
    end}

@example Creating a basic form

    form_for(@data, :method => :post) do |f|
      f.input_text 'Username', :username
    end

@example Adding custom elements inside a form

    form_for(@result, :method => :post) do |f|
      f.fieldset do
        f.g.div(:class => "control-group") do
          f.input_text 'Text label', :textname, { :placeholder => 'Chunky bacon!',
                                                  :class       => :bigsize }
        end
      end
    end

### Public Instance Methods

form_error(name, message) click to toggle source

Manually add a new error to the form_errors key in the flash hash. The first parameter is the name of the form field and the second parameter is the custom message.

@param [String] name The name of the form field to which the error

    belongs.

@param [String] message The custom error message to show.

    # File lib/ramaze/helper/blue_form.rb, line 107
    def form_error(name, message)
      if respond_to?(:flash)
        old = flash[:form_errors] || {}
        flash[:form_errors] = old.merge(name.to_s => message.to_s)
      else
        form_errors[name.to_s] = message.to_s
      end
    end

form_errors() click to toggle source

Returns the hash containing all existing errors and allows other methods to set new errors by using this method as if it were a hash.

@return [Array] All form errors.

    # File lib/ramaze/helper/blue_form.rb, line 122
    def form_errors
      if respond_to?(:flash)
        flash[:form_errors] ||= {}
      else
        @form_errors ||= {}
      end
    end

form_errors_from_model(obj) click to toggle source

Retrieve all the form errors for the specified model and add them to the flash hash.

@param [Object] obj An object of a model that contains form errors.

    # File lib/ramaze/helper/blue_form.rb, line 136
    def form_errors_from_model(obj)
      if obj.respond_to?(:errors)
        obj.errors.each do |key, value|
          if value.respond_to?(:first)
            value = value.first
          end

          form_error(key.to_s, value % key)
        end
      end
    end

form_for(form_values, options = {}, &block) click to toggle source

The form method generates the basic structure of the form. It should be called using a block and it's return value should be manually sent to the browser (since it does not echo the value).

@param [Object] form_values Object containing the values for each form

    field.

@param [Hash] options Hash containing any additional form attributes

    such as the method, action, enctype and so on.

@param [Block] block Block containing the elements of the form such as

    password fields, textareas and so on.

    # File lib/ramaze/helper/blue_form.rb, line 92
    def form_for(form_values, options = {}, &block)
      form = Form.new(form_values, options)
      form.build(form_errors, &block)
      form
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.