Kiss A simpler, smarter web application framework for Ruby

Kiss::Form::Field Subclass of Object

Attributes

cancelwritable

Not documented

columnswritable

Not documented

currencywritable

Not documented

display_formatwritable

Not documented

formwritable

Not documented

formatwritable

Not documented

hidden_joinwritable

Not documented

htmlwritable

Not documented

ignorewritable

Not documented

labelwritable

Not documented

namewritable

Not documented

no_labelwritable

Not documented

objectwritable

Not documented

optionswritable

Not documented

options_display_keywritable

Not documented

options_value_keywritable

Not documented

otherwritable

Not documented

other_fieldwritable

Not documented

promptwritable

Not documented

requiredwritable

Not documented

savewritable

Not documented

stylewritable

Not documented

typewritable

Not documented

valuewritable

Not documented

Public Class Methods

new(form,attrs) source

Not documented

    # File lib/kiss/form/field.rb, line 11
      def initialize(form,attrs)
        @form = form
        @save = true
        @currency = nil
        
        set_attributes({
          :type => :text,
          :object => @form.object,
          :save => true
        }.merge(attrs))
        
        @value = (@object[@name.to_sym] if @object) || @value
        
        @format = Kiss::Format.lookup(@format)
        @display_format = Kiss::Format.lookup(@display_format)
        
        @currency = '$' if @currency == :dollars
        
        @errors = []
      end
    

Public Instance Methods

add_error(message) source

Not documented

    # File lib/kiss/form/field.rb, line 112
      def add_error(message)
        @errors << message
        @form.has_field_errors = true
      end
    
column_layout(elements_html) source

Not documented

    # File lib/kiss/form/field.rb, line 56
      def column_layout(elements_html)
        if @columns
          # format elements into cells
          elements_html.map! { |el| "<td>" + el + "</td>" }
          
            # compute height = ceiling(number of elements / columns)
            height = ((elements_html.size + columns - 1)/columns).to_i
    
            # group cells into columns
            element_ranges = (1..height).to_a.map {|i| ((i-1)*@columns..(i*@columns-1))}
            return ['<table class="kiss_field_columns">',element_ranges.map do |range|
              ["<tr>", elements_html[range], "</tr>"]
            end,'</table>'].flatten.join
        end
        
        # else columns <= 1
        elements_html.join('&nbsp; ')
      end
    
content_tag_html(tag_name, content, attrs = {}, extra_html = nil) source

Not documented

    # File lib/kiss/form/field.rb, line 195
      def content_tag_html(tag_name, content, attrs = {}, extra_html = nil)
        tag_start_html(tag_name,attrs,extra_html) + ">#{content}</#{tag_name}>"
      end
    
display_to_s(value) source

Not documented

    # File lib/kiss/form/field.rb, line 121
      def display_to_s(value)
        value ? @display_format.value_to_s(value) : ''
      end
    
element_html() source

Not documented

    # File lib/kiss/form/field.rb, line 153
      def element_html
        attrs = { :value => param || value_to_s(@value) }
        if width = @format.input_width
          attrs[:style] = "width: #{width}px"
        end
        
        @currency.to_s + input_tag_html( attrs ) +
          ((legend = @format.legend) ? " <small>(#{legend})</small>" : '')
      end
    
errors_html() source

Not documented

    # File lib/kiss/form/field.rb, line 141
      def errors_html
        return nil unless @errors.size > 0
        
        if @errors.size == 1
          content = @errors[0]
        else
          content = "<ul>" + @errors.map {|e| "<li>#{e}</li>"}.join + "</ul>"
        end
        
        %Q(<span class="#{@form.field_error_class}">#{content}</span><br clear="all" />)
      end
    
html() source

Not documented

    # File lib/kiss/form/field.rb, line 163
      def html
        errors = errors_html
        element_html + (errors ? (@columns ? '' : '<br/>') + %Q(#{errors}) : '')
      end
    
input_tag_html(attrs = {}, extra_html = nil) source

Not documented

    # File lib/kiss/form/field.rb, line 199
      def input_tag_html(attrs = {}, extra_html = nil)
        tag_html(
          'input',
          {:type => @type}.merge(attrs),
          extra_html
        )
      end
    
other_field_html() source

Not documented

    # File lib/kiss/form/field.rb, line 32
      def other_field_html
        return '' unless @other
        
        other_checked = @value && !option_pairs.any? {|v,d| v == @value }
        
        (@columns ? '<br/>' : '&nbsp; ') + [
          input_tag_html(
            { :value => 'other', :html => { :id => @name+'.other' } },
            other_checked ? 'checked' : ''
          ),
          @other[:label] || 'Other',
          ': ',
          @currency.to_s,
          input_tag_html({
            :type => :text,
            :name => @name+'.other',
            :value => other_checked ? value_to_s(@value) : nil,
            :html => {
              :onfocus => "document.getElementById('#{@name}.other').checked = true"
            }
          }.merge(@other))
        ].join
      end
    
param() source

def sequel_value

  case @format
  when :date:
    return Kiss.mdy_to_ymd(value)
  when :datetime:
    datetime = value.sub(/([ap]m)\s*\Z/i,' \1')
    date, time, ampm = datetime.split(/\s+/)

    hours, minutes = time.split(/:/)
    hours = 0 if hours.to_i == 12 && ampm
    if (ampm.downcase == 'pm')
      hours = hours.to_i + 12
    end

    return Kiss.mdy_to_ymd(date) + " #{hours}:#{minutes}"
  when :month_year:
    month, year = value.sub(/\A\s*/,'').sub(/\s*\Z/,'').split(/\D+/)
    # convert two-digit years to four-digit years
    year = year.to_i
    if year < 100
      year += 1900
      year += 100 if year < Time.now.year - 95
    end
    return sprintf("%04d-%02d-01",year,month.to_i)
  else
    return value.is_a?(Array) ? value.map {|v| v.gsub(/,/,'\,')}.join(',') : value
  end

end

    # File lib/kiss/form/field.rb, line 104
      def param
        @param ||= @form.params[@name.to_s]
      end
    
tag_html(tag_name, attrs = {}, extra_html = nil) source

Not documented

    # File lib/kiss/form/field.rb, line 191
      def tag_html(tag_name, attrs = {}, extra_html = nil)
        tag_start_html(tag_name,attrs,extra_html) + ' />'
      end
    
tag_start_html(tag_name, attrs = {}, extra_html = nil) source

Not documented

    # File lib/kiss/form/field.rb, line 168
      def tag_start_html(tag_name, attrs = {}, extra_html = nil)
        attrs = attrs.clone
        attrs[:name] ||= @name
        attrs[:size] ||= @size if @size
        attrs[:style] ||= @style if @style
        
        html_parts = ["<#{tag_name}"]
        
        html = attrs.delete(:html) || @html
        attrs.each_pair {|k,v| html_parts.push %Q(#{k}="#{v}") }
        
        if html
          if html.is_a?(Hash)
            html.each_pair {|k,v| html_parts.push %Q(#{k}="#{v}") }
          else
            html_parts.push(@html.to_s)
          end
        end
        
        html_parts.push(extra_html) if extra_html
        html_parts.join(' ')
      end
    
validate(enter = 'enter') source

Not documented

    # File lib/kiss/form/field.rb, line 125
      def validate(enter = 'enter')
        if @required && (param !~ /\S/)
          # value required
          add_error("Please #{enter} #{@label}")
          return
        end
        
        begin
          @value = @format.validate(param)
        rescue Kiss::Format::ValidateError => e
          add_error("#{e.message.capitalize}")
        end
        
        @value
      end
    
value() source

Not documented

    # File lib/kiss/form/field.rb, line 108
      def value
        @value
      end
    
value_to_s(value) source

Not documented

    # File lib/kiss/form/field.rb, line 117
      def value_to_s(value)
        value ? @format.value_to_s(value) : ''
      end
    

Disabled; run with $DEBUG to generate this.