Class: Formtastic::Inputs::NumberInput

Inherits:
Object
  • Object
show all
Includes:
Base, Base::Stringish
Defined in:
lib/formtastic/inputs/number_input.rb

Overview

TODO:

Rename/Alias to NumberInput

Outputs a simple ‘<label>` with a HTML5 `<input type=“number”>` wrapped in the standard `<li>` wrapper. This is the default input choice for all database columns of the type `:float` and `:decimal`, as well as `:integer` columns that aren’t used for ‘belongs_to` associations, but can be applied to any text-like input with `:as => :number`.

Examples:

Full form context and output


<%= semantic_form_for(@user) do |f| %>
  <%= f.inputs do %>
    <%= f.input :shoe_size, :as => :number %>
  <% end %>
<% end %>

<form...>
  <fieldset>
    <ol>
      <li class="numeric">
        <label for="user_shoe_size">Shoe size</label>
        <input type="number" id="user_shoe_size" name="user[shoe_size]">
      </li>
    </ol>
  </fieldset>
</form>

Default HTML5 min/max/step attributes are detected from the numericality validations


class Person < ActiveRecord::Base
  validates_numericality_of :age, 
    :less_than => 100, 
    :greater_than => 17, 
    :only_integer => true
end

<%= f.input :age, :as => :number %>

<li class="numeric">
  <label for="persom_age">Age</label>
  <input type="number" id="person_age" name="person[age]" min="18" max="99" step="1">
</li>

Pass attributes down to the ‘<input>` tag

<%= f.input :shoe_size, :as => :number, :input_html => { :min => 3, :max => 15, :step => 1, :class => "special" } %>

See Also:

Direct Known Subclasses

NumericInput

Instance Attribute Summary

Attributes included from Base

#builder, #method, #object, #object_name, #options, #template

Instance Method Summary collapse

Methods included from Base::Stringish

#placeholder_text

Methods included from Base

#initialize

Methods included from Base::Wrapping

#input_wrapping, #wrapper_dom_id, #wrapper_html_options

Methods included from Base::Labelling

#label_from_options, #label_html, #label_html_options, #label_text, #localized_label, #render_label?, #requirement_text, #requirement_text_or_proc

Methods included from Base::Associations

#association, #association_primary_key, #belongs_to?, #reflection

Methods included from Base::Fileish

#file?

Methods included from Base::Validations

#column_limit, #limit, #not_required_through_negated_validation!, #not_required_through_negated_validation?, #optional?, #required?, #validation_integer_only?, #validation_limit, #validation_max, #validation_min, #validations, #validations?, #validator_relevant?

Methods included from Base::Naming

#as, #attributized_method_name, #humanized_method_name, #input_name, #sanitized_method_name, #sanitized_object_name

Methods included from Base::Hints

#hint?, #hint_html, #hint_text, #hint_text_from_options

Methods included from Base::Errors

#error_first_html, #error_html, #error_keys, #error_list_html, #error_none_html, #error_sentence_html, #errors, #errors?

Methods included from Base::Database

#column, #column?

Methods included from Base::Options

#formtastic_options, #input_options

Methods included from Base::Html

#dom_id, #dom_index

Instance Method Details

#input_html_optionsObject



62
63
64
65
66
67
68
# File 'lib/formtastic/inputs/number_input.rb', line 62

def input_html_options
  {
    :min => validation_min,
    :max => validation_max,
    :step => validation_integer_only? ? 1 : nil 
  }.merge(super)
end

#to_htmlObject



55
56
57
58
59
60
# File 'lib/formtastic/inputs/number_input.rb', line 55

def to_html
  input_wrapping do
    label_html <<
    builder.number_field(method, input_html_options)
  end
end