Class: FormInput

Inherits:
Object
  • Object
show all
Includes:
R18n::Helpers
Defined in:
lib/form_input/core.rb,
lib/form_input/r18n.rb,
lib/form_input/steps.rb,
lib/form_input/types.rb,
lib/form_input/version.rb,
lib/form_input/localize.rb

Overview

Version number.

Defined Under Namespace

Modules: StepMethods, Version Classes: Parameter

Constant Summary collapse

DEFAULT_SIZE_LIMIT =

Default size limit applied to all input.

255
DEFAULT_FILTER =

Default input filter applied to all input.

->{ gsub( /\s+/, ' ' ).strip }
DEFAULT_MIN_KEY =

Minimum hash key value we allow by default.

0
DEFAULT_MAX_KEY =

Maximum hash key value we allow by default.

( 1 << 64 ) - 1
DEFAULT_ENCODING =

Encoding we convert all input request parameters into.

Encoding::UTF_8
DEFAULT_ERROR_MESSAGES =

Hash mapping error codes to default error messages.

{
  required_scalar: "%p is required",
  required_array: "%p are required",
  not_array: "%p are not an array",
  not_hash: "%p are not a hash",
  not_string: "%p is not a string",
  match_key: "%p contain invalid key",
  invalid_key: "%p contain invalid key",
  min_key: "%p contain too small key",
  max_key: "%p contain too large key",
  min_count: "%p must have at least",
  max_count: "%p may have at most",
  value_type: "%p like this is not valid",
  element_type: "%p contain invalid value",
  min_limit: "%p must be at least",
  max_limit: "%p may be at most",
  inf_limit: "%p must be greater than",
  sup_limit: "%p must be less than",
  invalid_encoding: "%p must use valid encoding",
  invalid_characters: "%p may not contain invalid characters",
  min_size: "%p must have at least",
  max_size: "%p may have at most",
  min_bytesize: "%p must have at least",
  max_bytesize: "%p may have at most",
  reject_msg: "%p like this is not allowed",
  match_msg: "%p like this is not valid",
}
LATIN_NAMES_RE =

Matches names using latin alphabet.

/\A[\p{Latin}\-\. ]+\z/u
SIMPLE_EMAIL_RE =

Matches common email addresses. Note that it doesn't match all addresses allowed by RFC, though.

/\A[-_.=+%a-z0-9]+@(?:[-_a-z0-9]+\.)+[a-z]{2,4}\z/i
ZIP_CODE_RE =

Matches generic ZIP code. Note that the real format changes for each country.

/\A[A-Z\d]++(?:[- ]?[A-Z\d]+)*+\z/i
PHONE_NUMBER_FILTER =

Filter for phone numbers.

->{ gsub( /\s*[-\/\.]\s*/, '-' ).gsub( /\s+/, ' ' ).strip }
PHONE_NUMBER_RE =

Matches generic phone number.

/\A\+?\d++(?:[- ]?(?:\d+|\(\d+\)))*+(?:[- ]?[A-Z\d]+)*+\z/i
INTEGER_ARGS =

Integer number.

{
  filter: ->{ ( Integer( self, 10 ) rescue self ) unless empty? },
  class: Integer,
}
FLOAT_ARGS =

Float number.

{
  filter: ->{ ( Float( self ) rescue self ) unless empty? },
  class: Float,
}
BOOL_ARGS =

Boolean value, displayed as a select menu.

{
  type: :select,
  data: [ [ true, 'Yes' ], [ false, 'No' ] ],
  filter: ->{ self == 'true' unless empty? },
  class: [ TrueClass, FalseClass ],
}
CHECKBOX_ARGS =

Boolean value, displayed as a checkbox.

{
  type: :checkbox,
  filter: ->{ not empty? },
  format: ->{ self if self },
  class: [ TrueClass, FalseClass ],
}
EMAIL_ARGS =

Email.

{
  match: SIMPLE_EMAIL_RE,
}
ZIP_ARGS =

Zip code.

{
  match: ZIP_CODE_RE,
}
PHONE_ARGS =

Phone number.

{
  filter: PHONE_NUMBER_FILTER,
  match: PHONE_NUMBER_RE,
}
TIME_FORMAT =

Full time format.

"%Y-%m-%d %H:%M:%S".freeze
TIME_FORMAT_EXAMPLE =

Full time format example.

"YYYY-MM-DD HH:MM:SS".freeze
TIME_ARGS =

Full time.

{
  placeholder: TIME_FORMAT_EXAMPLE,
  filter: ->{ ( FormInput.parse_time!( self, TIME_FORMAT ) rescue self ) unless empty? },
  format: ->{ utc.strftime( TIME_FORMAT ) rescue self },
  class: Time,
}
US_DATE_FORMAT =

US date format.

"%m/%d/%Y".freeze
US_DATE_FORMAT_EXAMPLE =

US date format example.

"MM/DD/YYYY".freeze
US_DATE_ARGS =

Time in US date format.

{
  placeholder: US_DATE_FORMAT_EXAMPLE,
  filter: ->{ ( FormInput.parse_time!( self, US_DATE_FORMAT ) rescue self ) unless empty? },
  format: ->{ utc.strftime( US_DATE_FORMAT ) rescue self },
  class: Time,
}
UK_DATE_FORMAT =

UK date format.

"%d/%m/%Y".freeze
UK_DATE_FORMAT_EXAMPLE =

UK date format example.

"DD/MM/YYYY".freeze
UK_DATE_ARGS =

Time in UK date format.

{
  placeholder: UK_DATE_FORMAT_EXAMPLE,
  filter: ->{ ( FormInput.parse_time!( self, UK_DATE_FORMAT ) rescue self ) unless empty? },
  format: ->{ utc.strftime( UK_DATE_FORMAT ) rescue self },
  class: Time,
}
EU_DATE_FORMAT =

EU date format.

"%-d.%-m.%Y".freeze
EU_DATE_FORMAT_EXAMPLE =

EU date format example.

"D.M.YYYY".freeze
EU_DATE_ARGS =

Time in EU date format.

{
  placeholder: EU_DATE_FORMAT_EXAMPLE,
  filter: ->{ ( FormInput.parse_time!( self, EU_DATE_FORMAT ) rescue self ) unless empty? },
  format: ->{ utc.strftime( EU_DATE_FORMAT ) rescue self },
  class: Time,
}
HOURS_FORMAT =

Hours format.

"%H:%M".freeze
HOURS_FORMAT_EXAMPLE =

Hours format example.

"HH:MM".freeze
HOURS_ARGS =

Seconds since midnight in hours:minutes format.

{
  placeholder: HOURS_FORMAT_EXAMPLE,
  filter: ->{ ( FormInput.parse_time( self, HOURS_FORMAT ).to_i % 86400 rescue self ) unless empty? },
  format: ->{ Time.at( self ).utc.strftime( HOURS_FORMAT ) rescue self },
  class: Integer,
}
PRUNED_ARGS =

Transformation which drops empty values from hashes and arrays and turns empty string into nil.

{
  transform: ->{
    case self
    when Array
      reject{ |v| v.nil? or ( v.respond_to?( :empty? ) && v.empty? ) }
    when Hash
      reject{ |k,v| v.nil? or ( v.respond_to?( :empty? ) && v.empty? ) }
    when String
      self unless empty?
    else
      self
    end
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ FormInput

Create new form info, initializing it from given hash or request, if anything.



751
752
753
754
755
756
757
758
759
760
761
# File 'lib/form_input/core.rb', line 751

def initialize( *args )
  @params = bound_params
  @errors = nil
  for arg in args
    if arg.is_a? Hash
      set( arg )
    else
      import( arg )
    end
  end
end

Class Method Details

.[](*names) ⇒ Object

Get given parameter(s), hash style.



606
607
608
609
610
611
612
# File 'lib/form_input/core.rb', line 606

def []( *names )
  if names.count == 1
    form_params[ names.first ]
  else
    form_params.values_at( *names )
  end
end

.add(param) ⇒ Object

Add given parameter to the form, after performing basic validity checks.



615
616
617
618
619
620
621
622
623
624
# File 'lib/form_input/core.rb', line 615

def add( param )
  name = param.name

  fail ArgumentError, "duplicate parameter #{name}" if form_params[ name ]
  fail ArgumentError, "invalid parameter name #{name}" if method_defined?( name )

  self.send( :attr_accessor, name )

  form_params[ name ] = param
end

.array(name, *args, &block) ⇒ Object

Like param, except that it defines array parameter.



704
705
706
# File 'lib/form_input/core.rb', line 704

def array( name, *args, &block )
  param( name, *args, array: true, &block )
end

.array!(name, *args, &block) ⇒ Object

Like param!, except that it defines required array parameter.



709
710
711
# File 'lib/form_input/core.rb', line 709

def array!( name, *args, &block )
  param!( name, *args, array: true, &block )
end

.copy(source, opts = {}) ⇒ Object

Copy given/all form parameters. Returns self for chaining.



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/form_input/core.rb', line 629

def copy( source, opts = {} )
  case source
  when Parameter
    add( Parameter.new(
      opts[ :name ] || source.name,
      opts[ :code ] || opts[ :name ] || source.code,
      source.opts.merge( opts )
    ) )
  when Array
    source.each{ |x| copy( x, opts ) }
  when Class
    fail ArgumentError, "invalid source form #{source.inspect}" unless source < FormInput
    copy( source.form_params.values, opts )
  else
    fail ArgumentError, "invalid source parameter #{source.inspect}"
  end
  self
end

.default_translation(forms = self.forms) ⇒ Object

Get string containing YAML representation of the default R18n translation for all/given FormInput classes.



43
44
45
46
# File 'lib/form_input/localize.rb', line 43

def self.default_translation( forms = self.forms )
  hash = Hash[ forms.map{ |x| [ x.translation_name, x.translation_hash ] }.reject{ |k, v| v.empty? } ]
  YAML::dump( { forms: hash }.stringify_keys )
end

.define_steps(steps) ⇒ Object

Turn this form into multi-step form using given steps. Returns self for chaining.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/form_input/steps.rb', line 7

def self.define_steps( steps )
  @steps = steps = steps.to_hash.dup.freeze

  self.send( :include, StepMethods )

  opts = { filter: ->{ steps.keys.find{ |x| x.to_s == self } }, class: Symbol }

  param :step, opts, type: :hidden
  param :next, opts, type: :ignore
  param :last, opts, type: :hidden
  param :seen, opts, type: :hidden

  self
end

.find_inflection(string) ⇒ Object

Iterate over each possible inflection for given inflection string and return first non-nil result. You may override this if you need more complex inflection fallbacks for some locale.



129
130
131
132
133
134
135
# File 'lib/form_input/r18n.rb', line 129

def self.find_inflection( string )
  until string.empty?
    break if result = yield( string )
    string = string[0..-2]
  end
  result
end

.form_paramsObject

Get hash mapping parameter names to parameters themselves.



601
602
603
# File 'lib/form_input/core.rb', line 601

def form_params
  @params ||= {}
end

.form_stepsObject

Get hash mapping defined steps to their names, or nil if there are none.



23
24
25
# File 'lib/form_input/steps.rb', line 23

def self.form_steps
  @steps
end

.formsObject

Get list of all classes inherited from FormInput.



38
39
40
# File 'lib/form_input/localize.rb', line 38

def self.forms
  ObjectSpace.each_object( Class ).select{ |x| x < FormInput and x.name }.sort_by{ |x| x.name }
end

.from_hash(hash) ⇒ Object

Create new form from hash with internal values.



734
735
736
# File 'lib/form_input/core.rb', line 734

def from_hash( hash )
  new.set( hash )
end

.from_params(params) ⇒ Object

Create new form from hash with external values.



729
730
731
# File 'lib/form_input/core.rb', line 729

def from_params( params )
  new.import( params )
end

.from_request(request) ⇒ Object

Create new form from request with external values.



724
725
726
# File 'lib/form_input/core.rb', line 724

def from_request( request )
  new.import( request )
end

.hash(name, *args, &block) ⇒ Object

Like param, except that it defines hash parameter.



714
715
716
# File 'lib/form_input/core.rb', line 714

def hash( name, *args, &block )
  param( name, *args, hash: true, &block )
end

.hash!(name, *args, &block) ⇒ Object

Like param!, except that it defines required hash parameter.



719
720
721
# File 'lib/form_input/core.rb', line 719

def hash!( name, *args, &block )
  param!( name, *args, hash: true, &block )
end

.inherited(into) ⇒ Object

Create standalone copy of form parameters in case someone inherits an existing form.



596
597
598
# File 'lib/form_input/core.rb', line 596

def inherited( into )
  into.instance_variable_set( '@params', form_params.dup )
end

.param(name, *args, &block) ⇒ Object

Define form parameter with given name, code, title, maximum size, options, and filter block. All fields except name are optional. In case the code is missing, name is used instead. If no size limits are specified, 255 characters and bytes limits are applied by default. If no filter is explicitly defined, default filter squeezing and stripping whitespace is applied. Returns self for chaining.



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'lib/form_input/core.rb', line 653

def param( name, *args, &block )

  # Fetch arguments.

  code = name
  code = args.shift if args.first.is_a? Symbol

  title = args.shift if args.first.is_a? String

  size = args.shift if args.first.is_a? Numeric

  opts = {}
  opts.merge!( args.shift ) while args.first.is_a? Hash

  fail ArgumentError, "invalid arguments #{args}" unless args.empty?

  # Set the title.

  opts[ :title ] = title.freeze if title

  # Set input filter.

  opts[ :filter ] = block if block
  opts[ :filter ] = DEFAULT_FILTER unless opts.key?( :filter )

  # Enforce default size limits for any input processed.

  limit = DEFAULT_SIZE_LIMIT

  size = ( opts[ :max_size ] ||= size || limit )
  opts[ :max_bytesize ] ||= limit if size.is_a?( Proc ) or size <= limit

  # Set default key limits for hash parameters.

  if opts[ :hash ]
    opts[ :min_key ] ||= DEFAULT_MIN_KEY
    opts[ :max_key ] ||= DEFAULT_MAX_KEY
  end

  # Define parameter.

  add( Parameter.new( name, code, opts ) )
  self
end

.param!(name, *args, &block) ⇒ Object

Like param, except this defines required parameter.



699
700
701
# File 'lib/form_input/core.rb', line 699

def param!( name, *args, &block )
  param( name, *args, required: true, &block )
end

.parse_time(string, format) ⇒ Object

Parse time like Time#strptime but raise on trailing garbage. Also ignores -, _ and ^ % modifiers, so the same format can be used for both parsing and formatting.



143
144
145
146
147
148
149
# File 'lib/form_input/types.rb', line 143

def self.parse_time( string, format )
  format = format.gsub( /%[-_^]?(.)/, '%\1' )
  # Rather than using _strptime and checking the leftover field,
  # add required trailing character to both the string and format parameters.
  suffix = ( string[ -1 ] == "\1" ? "\2" : "\1" )
  Time.strptime( "+0000 #{string}#{suffix}", "%z #{format}#{suffix}" ).utc
end

.parse_time!(string, format) ⇒ Object

Like parse_time, but falls back to DateTime.parse heuristics when the date/time can't be parsed.



152
153
154
155
156
# File 'lib/form_input/types.rb', line 152

def self.parse_time!( string, format )
  parse_time( string, format )
rescue
  DateTime.parse( string ).to_time.utc
end

.translation_hashObject

Get hash of all form values which may need to be localized.



31
32
33
34
35
# File 'lib/form_input/localize.rb', line 31

def self.translation_hash
  hash = Hash[ form_params.map{ |k, v| [ k, v.translation_hash ] }.reject{ |k, v| v.empty? } ]
  hash[ :steps ] = form_steps.reject{ |k, v| v.nil? } if form_steps
  hash
end

.translation_nameObject

Get name of the form used as translation scope for text translations.



112
113
114
115
116
117
# File 'lib/form_input/r18n.rb', line 112

def self.translation_name
  @translation_name ||= name.split( '::' ).last
    .gsub( /([A-Z]+)([A-Z][a-z])/, '\1_\2' )
    .gsub( /([a-z\d])([A-Z])/, '\1_\2' )
    .downcase
end

.translations_pathObject

Get path to R18n translations provided by this gem.



107
108
109
# File 'lib/form_input/r18n.rb', line 107

def self.translations_path
  File.expand_path( "#{__FILE__}/../r18n" )
end

Instance Method Details

#[](*names) ⇒ Object

Get given parameter(s) value(s), hash style.



852
853
854
855
856
857
858
# File 'lib/form_input/core.rb', line 852

def []( *names )
  if names.count == 1
    send( names.first )
  else
    names.map{ |x| send( x ) }
  end
end

#[]=(name, value) ⇒ Object

Set given parameter value, hash style. Unlike setting the attribute directly, this triggers a revalidation in the future.



862
863
864
865
# File 'lib/form_input/core.rb', line 862

def []=( name, value )
  @errors = nil
  send( "#{name}=", value )
end

#array_paramsObject Also known as: array_parameters

Get list of array parameters.



1007
1008
1009
# File 'lib/form_input/core.rb', line 1007

def array_params
  params.select{ |x| x.array? }
end

#blank_paramsObject Also known as: blank_parameters

Get list of parameters with blank values.



947
948
949
# File 'lib/form_input/core.rb', line 947

def blank_params
  params.select{ |x| x.blank? }
end

#build_url(url, args = {}) ⇒ Object

Build URL from given URL and combination of current paramaters and provided parameters.



1084
1085
1086
# File 'lib/form_input/core.rb', line 1084

def build_url( url, args = {} )
  dup.set( args ).extend_url( url )
end

#chunked_params(params = self.params) ⇒ Object

Get all/given parameters chunked into individual rows for nicer form display.



1049
1050
1051
# File 'lib/form_input/core.rb', line 1049

def chunked_params( params = self.params )
  params.chunk{ |p| p[ :row ] || :_alone }.map{ |x,a| a.count > 1 ? a : a.first }
end

#clear(*names) ⇒ Object

Clear all/given parameter values. Both names and parameters are accepted. Returns self for chaining.



843
844
845
846
847
848
849
# File 'lib/form_input/core.rb', line 843

def clear( *names )
  names = names.empty? ? params_names : validate_names( names )
  for name in names
    self[ name ] = nil
  end
  self
end

#correct_paramsObject Also known as: correct_parameters

Get list of parameters with correct value types.



935
936
937
# File 'lib/form_input/core.rb', line 935

def correct_params
  params.select{ |x| x.correct? }
end

#disabled_paramsObject Also known as: disabled_parameters

Get list of disabled parameters.



977
978
979
# File 'lib/form_input/core.rb', line 977

def disabled_params
  params.select{ |x| x.disabled? }
end

#empty?Boolean

Return true if all parameters are empty.

Returns:

  • (Boolean)


1056
1057
1058
# File 'lib/form_input/core.rb', line 1056

def empty?
  filled_params.empty?
end

#empty_paramsObject Also known as: empty_parameters

Get list of parameters with empty values.



953
954
955
# File 'lib/form_input/core.rb', line 953

def empty_params
  params.select{ |x| x.empty? }
end

#enabled_paramsObject Also known as: enabled_parameters

Get list of enabled parameters.



983
984
985
# File 'lib/form_input/core.rb', line 983

def enabled_params
  params.select{ |x| x.enabled? }
end

#error_for(name) ⇒ Object

Get first error for given parameter. Returns nil if there were no errors.



1115
1116
1117
# File 'lib/form_input/core.rb', line 1115

def error_for( name )
  errors_for( name ).first
end

#error_messagesObject

Get list of error messages, but including only the first one reported for each parameter.



1097
1098
1099
# File 'lib/form_input/core.rb', line 1097

def error_messages
  errors.values.map{ |x| x.first }
end

#errorsObject

Get hash of all errors detected for each parameter.



1091
1092
1093
1094
# File 'lib/form_input/core.rb', line 1091

def errors
  validate?
  @errors.dup
end

#errors_for(name) ⇒ Object

Get list of errors for given parameter. Returns empty list if there were no errors.



1110
1111
1112
# File 'lib/form_input/core.rb', line 1110

def errors_for( name )
  errors[ name ] || []
end

#except(*names) ⇒ Object

Create copy of itself, with given parameters unset. Both names and parameters are accepted.



887
888
889
890
891
892
893
# File 'lib/form_input/core.rb', line 887

def except( *names )
  result = dup
  for name in validate_names( names )
    result[ name ] = nil
  end
  result
end

#extend_url(url) ⇒ Object

Extend given URL with query created from all current non-empty parameters.



1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/form_input/core.rb', line 1074

def extend_url( url )
  url = url.to_s.dup
  query = url_query
  unless query.empty?
    url << ( url['?'] ? '&' : '?' ) << query
  end
  url
end

#filled_paramsObject Also known as: filled_parameters

Get list of parameters with non-empty values.



959
960
961
# File 'lib/form_input/core.rb', line 959

def filled_params
  params.select{ |x| x.filled? }
end

#freezeObject

Freeze the form.



778
779
780
781
782
783
784
# File 'lib/form_input/core.rb', line 778

def freeze
  unless frozen?
    validate?
    @errors.freeze.each{ |k,v| v.freeze }
  end
  super
end

#ft(*args) ⇒ Object

Like t helper, except that the translation is looked up in the forms. scope. Supports both ft.name( args ) and ft( :name, args ) forms.



121
122
123
124
125
# File 'lib/form_input/r18n.rb', line 121

def ft( *args )
  fail "You need to set the locale with R18n.set('en') or similar. No locale, no helper. Sorry." unless r18n
  translation = t.forms[ self.class.translation_name ]
  args.empty? ? translation : translation[ *args ]
end

#hash_paramsObject Also known as: hash_parameters

Get list of hash parameters.



1013
1014
1015
# File 'lib/form_input/core.rb', line 1013

def hash_params
  params.select{ |x| x.hash? }
end

#hidden_paramsObject Also known as: hidden_parameters

Get list of hidden parameters.



989
990
991
# File 'lib/form_input/core.rb', line 989

def hidden_params
  params.select{ |x| x.hidden? }
end

#ignored_paramsObject Also known as: ignored_parameters

Get list of ignored parameters.



995
996
997
# File 'lib/form_input/core.rb', line 995

def ignored_params
  params.select{ |x| x.ignored? }
end

#import(request) ⇒ Object

Import parameter values from given request or hash. Applies parameter input filters and transforms as well. Returns self for chaining.



819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/form_input/core.rb', line 819

def import( request )
  for name, param in @params
    if value = request[ param.code ]
      value = sanitize_value( value, param.filter )
      if transform = param.transform
        value = value.instance_exec( &transform )
      end
      self[ name ] = value
    end
  end
  self
end

#incorrect_paramsObject Also known as: incorrect_parameters

Get list of parameters with incorrect value types.



941
942
943
# File 'lib/form_input/core.rb', line 941

def incorrect_params
  params.select{ |x| x.incorrect? }
end

#initialize_clone(other) ⇒ Object

Initialize form clone.



764
765
766
767
768
# File 'lib/form_input/core.rb', line 764

def initialize_clone( other )
  super
  @params = bound_params
  @errors &&= Hash[ @errors.map{ |k,v| [ k, v.clone ] } ]
end

#initialize_dup(other) ⇒ Object

Initialize form copy.



771
772
773
774
775
# File 'lib/form_input/core.rb', line 771

def initialize_dup( other )
  super
  @params = bound_params
  @errors = nil
end

#invalid?(*names) ⇒ Boolean

Test if there were some errors (overall or for given parameters) reported.

Returns:

  • (Boolean)


1129
1130
1131
# File 'lib/form_input/core.rb', line 1129

def invalid?( *names )
  not valid?( *names )
end

#invalid_paramsObject Also known as: invalid_parameters

Get list of parameters with some errors reported.



1043
1044
1045
# File 'lib/form_input/core.rb', line 1043

def invalid_params
  params.select{ |x| x.invalid? }
end

#named_params(*names) ⇒ Object Also known as: named_parameters

Get list of given named parameters. Note that nil is returned for unknown names, and duplicate parameters for duplicate names.



929
930
931
# File 'lib/form_input/core.rb', line 929

def named_params( *names )
  @params.values_at( *names )
end

#only(*names) ⇒ Object

Create copy of itself, with only given parameters set. Both names and parameters are accepted.



896
897
898
899
900
901
902
903
904
905
# File 'lib/form_input/core.rb', line 896

def only( *names )
  # It would be easier to create new instance here and only copy selected values,
  # but we want to use dup instead of new here, as the derived form can use
  # different parameters in its construction.
  result = dup
  for name in params_names - validate_names( names )
    result[ name ] = nil
  end
  result
end

#optional_paramsObject Also known as: optional_parameters

Get list of optional parameters.



971
972
973
# File 'lib/form_input/core.rb', line 971

def optional_params
  params.select{ |x| x.optional? }
end

#param(name) ⇒ Object Also known as: parameter

Get given named parameter.



910
911
912
# File 'lib/form_input/core.rb', line 910

def param( name )
  @params[ name ]
end

#paramsObject Also known as: parameters

Get list of all parameters.



916
917
918
# File 'lib/form_input/core.rb', line 916

def params
  @params.values
end

#params_namesObject Also known as: parameters_names

Get list of all parameter names.



922
923
924
# File 'lib/form_input/core.rb', line 922

def params_names
  @params.keys
end

#report(name, msg) ⇒ Object

Remember error concerning given parameter. Returns self for chaining.



1103
1104
1105
1106
1107
# File 'lib/form_input/core.rb', line 1103

def report( name, msg )
  validate?
  ( @errors[ name ] ||= [] ) << msg.to_s.dup.freeze
  self
end

#required_paramsObject Also known as: required_parameters

Get list of required parameters.



965
966
967
# File 'lib/form_input/core.rb', line 965

def required_params
  params.select{ |x| x.required? }
end

#scalar_paramsObject Also known as: scalar_parameters

Get list of scalar parameters.



1019
1020
1021
# File 'lib/form_input/core.rb', line 1019

def scalar_params
  params.select{ |x| x.scalar? }
end

#set(hash) ⇒ Object

Set parameter values from given hash. Returns self for chaining.



834
835
836
837
838
839
# File 'lib/form_input/core.rb', line 834

def set( hash )
  for name, value in hash
    self[ name ] = value
  end
  self
end

#tagged_params(*tags) ⇒ Object Also known as: tagged_parameters

Get list of parameters tagged with given/any tags.



1025
1026
1027
# File 'lib/form_input/core.rb', line 1025

def tagged_params( *tags )
  params.select{ |x| x.tagged?( *tags ) }
end

#to_hashObject Also known as: to_h

Return all non-empty parameters as a hash. See also url_params, which creates a hash suitable for url output.



869
870
871
872
873
# File 'lib/form_input/core.rb', line 869

def to_hash
  result = {}
  filled_params.each{ |x| result[ x.name ] = x.value }
  result
end

#untagged_params(*tags) ⇒ Object Also known as: untagged_parameters

Get list of parameters not tagged with given/any tags.



1031
1032
1033
# File 'lib/form_input/core.rb', line 1031

def untagged_params( *tags )
  params.select{ |x| x.untagged?( *tags ) }
end

#url_paramsObject Also known as: url_parameters

Get hash of all non-empty parameters for use in URL.



1061
1062
1063
1064
1065
# File 'lib/form_input/core.rb', line 1061

def url_params
  result = {}
  filled_params.each{ |x| result[ x.code ] = x.form_value }
  result
end

#url_queryObject

Create string containing URL query from all current non-empty parameters.



1069
1070
1071
# File 'lib/form_input/core.rb', line 1069

def url_query
  Rack::Utils.build_nested_query( url_params )
end

#valid(name, *names) ⇒ Object

Return parameter(s) value(s) as long as they are all valid, nil otherwise.



1134
1135
1136
# File 'lib/form_input/core.rb', line 1134

def valid( name, *names )
  self[ name, *names ] if valid?( name, *names )
end

#valid?(*names) ⇒ Boolean

Test if there were no errors (overall or for given parameters) reported.

Returns:

  • (Boolean)


1120
1121
1122
1123
1124
1125
1126
# File 'lib/form_input/core.rb', line 1120

def valid?( *names )
  if names.empty?
    errors.empty?
  else
    validate_names( names ).all?{ |x| errors_for( x ).empty? }
  end
end

#valid_paramsObject Also known as: valid_parameters

Get list of parameters with no errors reported.



1037
1038
1039
# File 'lib/form_input/core.rb', line 1037

def valid_params
  params.select{ |x| x.valid? }
end

#validateObject

Validate parameter values and remember any errors detected. You can override this in your class if you need more specific validation and :check callback is not good enough. Just make sure to call super first. Returns self for chaining.



1142
1143
1144
1145
1146
# File 'lib/form_input/core.rb', line 1142

def validate
  @errors ||= {}
  params.each{ |x| x.validate }
  self
end

#validate!Object

Like validate, except that it forces revalidation of all parameters. Returns self for chaining.



1150
1151
1152
1153
1154
# File 'lib/form_input/core.rb', line 1150

def validate!
  @errors = {}
  validate
  self
end

#validate?Boolean

Like validate, except that it does nothing if validation was already done. Returns self for chaining.

Returns:

  • (Boolean)


1158
1159
1160
1161
# File 'lib/form_input/core.rb', line 1158

def validate?
  validate unless @errors
  self
end

#visible_paramsObject Also known as: visible_parameters

Get list of visible parameters.



1001
1002
1003
# File 'lib/form_input/core.rb', line 1001

def visible_params
  params.select{ |x| x.visible? }
end