Class: Kiss::Form::MultiValueField

Inherits:
MultiChoiceField show all
Defined in:
lib/kiss/form/field.rb

Overview

Subclass for field types in which multiple values can be chosen from a set of options.

Direct Known Subclasses

CheckboxField, MultiSelectField

Constant Summary

Constants inherited from Field

Field::CURRENCY_SYMBOLS

Instance Method Summary collapse

Methods inherited from MultiChoiceField

#column_layout, #display_to_s, #form=, #has_option_value?, #initialize, #option_pairs, #options_keys, #other_field_html

Methods inherited from Field

#add_error, #content_tag_html, #debug, #element_html, #errors_html, #html, #initialize, #input_tag_html, #method_missing, #require_value, #reset, #set_value_to_hash, #set_value_to_object, #tag_html, #tag_start_html, #tip_html, #type, #value, #value_string, #value_to_s

Constructor Details

This class inherits a constructor from Kiss::Form::MultiChoiceField

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Kiss::Form::Field

Instance Method Details

#paramObject



439
440
441
# File 'lib/kiss/form/field.rb', line 439

def param
  @_form.params[@_name + '[]'] || []
end

#selected_option_valuesObject



469
470
471
# File 'lib/kiss/form/field.rb', line 469

def selected_option_values
  @_selected_option_values ||= @_value ? Hash[ *(@_value.map {|v| [value_to_s(v), true]}.flatten) ] : {}
end

#validateObject



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/kiss/form/field.rb', line 443

def validate
  begin
    @_value = param.map { |p| @_format.validate(p) }
  rescue Kiss::Format::ValidateError => e
    return add_error("#{e.message.capitalize}.")
  end
  
  if @_value.empty? && @_required
    return add_error "Please select at least one #{@_label.downcase.singularize}."
  end
  
  if @_min_value_size && @_value.size < @_min_value_size
    return add_error "Please select at least #{@_min_value_size.of(@_label.downcase)}."
  end
  
  if @_max_value_size && @_value.size > @_max_value_size
    return add_error "Please select no more than #{@_max_value_size.of(@_label.downcase)}."
  end
  
  @_value.each do |v|
    unless has_option_value?(v)
      return add_error "Invalid selection"
    end
  end
end