Method: Kiss::Form::Field#initialize

Defined in:
lib/kiss/form/field.rb

#initialize(form, *args, &block) ⇒ Field

Returns a new instance of Field.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kiss/form/field.rb', line 64

def initialize(form, *args, &block)
  # call as method to allow subclasses to override
  self.form = form
  
  # defaults
  @_save = true
  @_currency = nil
  @_object = @_form.object
  @_attach_errors = true
  
  _instance_variables_set_from_attrs(args.to_attrs)
  instance_eval(&block) if block_given?
  
  raise 'field must have a name' unless @_name
  @_name = @_name
  @_key ||= @_name.to_sym
  @_label ||= @_name.titleize unless self.is_a?(SubmitField)
  
  @_errors = []
  @_format = Kiss::Format.lookup(@_format)
  @_tip = ((legend = @_format.legend) ? "(#{legend})" : nil) unless defined? @_tip
  
  # object's value (if present) overrides default form field value
  if @_object && (value = @_object[@_key])
    @_value = value
  end
  
  if @_currency.is_a?(Symbol)
    @_currency = CURRENCY_SYMBOLS[@_currency] || ''
  end
  
  @_form.has_required_fields ||= @_required
end