class Aerogel::Forms::FormField
Field represents a field of form object.
Constants
- KNOWN_OPTIONS
Known options to a field are processed, the rest (unknown options) goes as html params.
Attributes
data_object[RW]
form_object[RW]
name[RW]
options[RW]
Public Class Methods
new( form_object, name, options = {} )
click to toggle source
# File lib/aerogel/forms/form_field.rb, line 13 def initialize( form_object, name, options = {} ) #unless name.is_a? Symbol # raise ArgumentError.new "Field name should be a Symbol" #end self.form_object = form_object self.data_object = form_object.object self.name = name.to_sym self.options = options end
Public Instance Methods
css_id()
click to toggle source
Returns unique css id for the field.
# File lib/aerogel/forms/form_field.rb, line 98 def css_id html_name.parameterize+"-#{form_object.id}" end
default_value()
click to toggle source
Returns field default value.
# File lib/aerogel/forms/form_field.rb, line 78 def default_value return options[:default_value] if options[:default_value] case type when :checkbox 1 else nil end end
errors()
click to toggle source
Returns list of error messages for the field.
# File lib/aerogel/forms/form_field.rb, line 112 def errors data_object.errors.get name end
hint()
click to toggle source
Returns input field hint — a piece of short explanation rendered beside the field.
# File lib/aerogel/forms/form_field.rb, line 55 def hint return @hint if @hint @hint = options[:hint] @hint = data_object.fields[name.to_s].hint if data_object.respond_to?( :fields ) && data_object.fields[name.to_s].respond_to?( :hint ) @hint end
html_name( use_name = nil )
click to toggle source
Returns Rack-parseable form field name.
# File lib/aerogel/forms/form_field.rb, line 90 def html_name( use_name = nil ) prefix = form_object.field_prefix use_name ||= name prefix.nil? ? use_name.to_s : "#{prefix}[#{use_name}]" end
html_params()
click to toggle source
Returns a string of html params for the <input …> tag.
# File lib/aerogel/forms/form_field.rb, line 104 def html_params attrs = @options.except( *KNOWN_OPTIONS ) attrs = attrs.deep_merge( @options[:html_params] ) if @options.key? :html_params attrs.map{|n, v| v.nil? ? "#{n}" : "#{n}=\"#{v}\""}.join(" ") end
invalid?()
click to toggle source
Returns true if the field is invalid
# File lib/aerogel/forms/form_field.rb, line 124 def invalid? not valid? end
is_collection?()
click to toggle source
Returns true if the field is actually a collection in parent object.
# File lib/aerogel/forms/form_field.rb, line 25 def is_collection? data_object.send( name ).is_a? Array end
label()
click to toggle source
Returns input field label.
# File lib/aerogel/forms/form_field.rb, line 43 def label return @label if @label @label = options[:label] @label ||= data_object.fields[name.to_s].label if data_object.respond_to?( :fields ) && data_object.fields[name.to_s] @label ||= data_object.class.human_attribute_name( name.to_s, default: '' ) if data_object.class.respond_to?( :human_attribute_name ) @label = nil if @label == '' @label ||= I18n.t "aerogel.forms.attributes.#{name}", default: name.to_s.humanize @label end
placeholder()
click to toggle source
Returns placeholder for the field, defaulting to label.
# File lib/aerogel/forms/form_field.rb, line 64 def placeholder options[:placeholder] || label end
required?()
click to toggle source
Returns true if the field is required.
# File lib/aerogel/forms/form_field.rb, line 130 def required? return options[:required] if !options[:required].nil? if data_object.class.respond_to? :validators_on vv = data_object.class.validators_on( name ).map(&:class) vv.include?( Mongoid::Validations::PresenceValidator ) else false end end
type()
click to toggle source
Returns input field type as a Symbol.
# File lib/aerogel/forms/form_field.rb, line 31 def type return @type if @type @type = options[:as] if data_object.respond_to? :fields @type ||= data_object.fields[name.to_s].type.name.parameterize.to_sym rescue nil end @type ||= :string @type end
valid?()
click to toggle source
Returns true if the field is valid.
# File lib/aerogel/forms/form_field.rb, line 118 def valid? form_object.valid? || errors.nil? end
value()
click to toggle source
Returns field value.
# File lib/aerogel/forms/form_field.rb, line 70 def value # return nil unless data_object.respond_to? :fields return options[:value] if options[:value] data_object.send name rescue default_value end