Preface
This page contains documentation pertaining to a Ruby Gem that I've created: form_input_field. This gem is an extension to ActionView::Helpers::FormHelper within Ruby on Rails' ActionPack framework. form_input_field essentially wraps up functionality of maintaining values for forms to factor cases where a POST fails model validation. It also provides a means to succinctly produce relevant error messages for said failed validations.
Consider a simple form which is primarily defined by an HTML input element and its label:
Consider added functionality which presents a new label informing the user any errors discovered upon POST whilst also maintaining the value the user had previously submitted:
It's important to validate such fields on the server, even if JavaScript is doing this work on the front-end. Within Ruby on Rails, (using HAML), the logic may look like the following:
This gem condenses the above into a more concise set of method calls:
This makes a view much more clean and easier to digest at a glance.
This gem was influenced by work done in a collaborative Ruby on Rails project. The goal was to abstract away code related to the front-end as much as possible in an effort to create a coding standard. Details pertaining to why and how can be found within the project writing within this website called Developing DRY Forms.
The source code of this gem is found within its GitHub repository. The primary body of this project page will act as a hub for the documentation found within this repository where the contents of the README file and the CHANGELOG file will be presented in tandem with a checklist of current and future progress.
The location of the gem is on RubyGems.org, located here: https://rubygems.org/gems/form_input_field
Ruby Gem - form_input_field
ActionView::Helpers::FormHelper::FormInputField
Installation
Add this line to your application's Gemfile:
- gem 'form_input_field'
And then execute:
- $ bundle
Or install it yourself as:
- $ gem install form_input_field
Usage
This gem places two helper methods into ActionView::Helpers::FormHelper. These methods are form_input_field and form_error_field.
These methods are described below. Examples of their usage are located in the Examples Section of this document.
form_input_field
form_input_field is an abstraction on the software pattern which encapsulates some input element and its label. Additionally, it has built in functionality which helps ensure the input element's value attribute is filled should the flash hash-map contain the presence of this value.
-
Outputs two html tags - a label HTML element pointing to its corresponding input HTML element. The term "input HTML element" here is ambiguous; it's not meant to be taken literally. "Input" in this context is meant to be interpreted as any output produced by the following list of helper functions within
ActionView::Helpers::FormHelper:- color_field, date_field, datetime_field, datetime_local_field, email_field, file_field, hidden_field, month_field, number_field, password_field, phone_field, range_field, search_field, telephone_field, text_area, text_field, time_field, url_field, week_field
-
In addition to the above list of
FormHelpermethod calls,form_input_fieldcaptures two special cases - check_box and radio_button. These are described later.
-
The
helper_symargument describes the relevant helper method to be called. It expects the method name as a symbol. I.e., if one needs a call to text_field, pass:text_field; If one needs a call to password_field, supply a value of:password_field, etc. -
The
object_nameandmethodarguments correspond to the equivalently named arguments as described inActionView::Helpers::FormHelper. -
The
label_textargument expects a string for the associated label of the generated input HTML element described by helper_sym. Supplying a false will instead not produce a label HTML element; an empty string is produced for the label. -
The
optionsargument corresponds to a hash-map representing the set of options to be passed withhelper_sym; these are the options to be given to a method call from the above set of helper methods. Esentially a hash-map of html properties and attributes. I.e.,{:style => "color:red;"}. -
The
label_optionsargument corresponds to a hash-map representing the set of options to be passed with the call to the label helper method fromActionView::Helpers::FormHelper. Essentially a hash-map of html properties and attributes to be applied to the label. I.e.,{:style => "color:red;"}. -
The
value_keyargument is a symbol that acts as a key for the flash hash-map that contains the relevant value filled by the controller. If the value associated with said key within flash is a string, then the string will occupy thevalueattribute for the produced input HTML element. If it is a hash-map, it will then assume that the value given for method is the key to the string within this embedded hash-map. Consider the following example:-
For a view that contains the following call:
form_input_field :person, :name, "Please input a name: ", the controller contains eitherflash[:values] = params[:user][:name]orflash[:values] = params[:user]whenUser.new(params[:user][:name]).valid?returns a false.
-
For a view that contains the following call:
:check_box helper_sym
When using the value of :check_box for helper_sym, the argument set is as follows:
-
The
checked_valueandunchecked_valuearguments correspond to the value sent to the server upon post, dependent whether or not the check box is checked. - The other arguments are unchanged from their descriptions above.
:radio_button helper_sym
When using the value of :radio_button for helper_sym, the argument set is as follows:
-
The
tag_valueargument corresponds to the value that will be sent to the server upon post, dependent whether or not the radio_button is selected. - The other arguments are unchanged from their descriptions above.
form_error_field
form_error_field is an abstraction on the software pattern which encapsulates the presentation of a validation error. This produces a label HTML element which points to the originating input field. The text of the label is the error message associated with the failed validation. These errors are typically captured from the model by the controller and then sent to the view where this method call is enacted.
-
Outputs a label HTML tag whose textual value is located in the flash-hash map corresponding to
error_key. If the value associated with said key is a string, then this string value will be used. If it is a hash-map, it will then assume that the value given formethodis the key to the string within this embedded hash-map. Consider the following example:-
For a view that contains the following call:
form_error_field :person, :name, the controller contains eitherflash[:errors] = @user.errors[:name]orflash[:errors] = @user.errors, where@useris defined byUser.new(params[:user][:name])andUser.valid?returns afalse.
-
For a view that contains the following call:
-
The
label_optionsargument corresponds to a hash-map representing the set of options to be passed with the call to the label helper function fromActionView::Helpers::FormHelper. Essentially a hash-map of html properties and attributes. I.e.,{:style => "color:red;"}.
Examples
form_input_field
form_error_field:
Finished Work
-
Base definition of
form_input_fieldsuch that its optional keyword arguments can be supplied in any order. -
Base definition of
form_error_fieldsuch that its optional keyword arguments can be supplied in any order.
Future Work
- A mechanism to set flash key defaults.
-
Extend these helpers to be compatible with
ActionView::Helpers::FormBuilder. -
Create function wrappers specific to the required
ActionView::Helpers::FormHelpermethod. I.e.,form_text_field(:model, :object_name)which callsform_input_field(:text_field, :model, :object_name)
Other avenues of future work will be listed in the issues section of the repository
Change Log
- 0.8.57 - Changes to gemspec file to include information for the changelog. Updated the readme file to include some examples.
-
0.8.56 - Fixed output error pertaining to concatenation of label string and input string for
form_input_field. Inclusion of an initial suite of tests capturing the examples given in ActionView docs. -
0.8.55 - Initial push to RubyGems.org. Implementation of
form_input_fieldandform_error_field. Implementation of a suite of tests with respect to an instantiation of an ActionView object.