Module FormValidator::InputProfile |
|
This module contains all the valid methods that can be invoked from an input profile. See the method definitions below for more information.
Methods |
Public Instance methods |
required() |
Takes an array, symbol, or string.
Any fields in this list which are not present in the user input will be reported as missing.
:required => [:name, :age, :phone]
optional() |
Takes an array, symbol, or string.
Any fields in this list which are present in the form hash will go through any specified constraint checks and filters. Any fields that aren't in the optional or required list are reported as unknown and deleted from the valid hash.
:optional => [:name, :age, :phone]
required_regexp() |
Takes a regular expression.
Specifies additional fieds which are required. If a given form element matches the regexp, it must have data, or it will be reported in the missing field list.
:required_regexp => /name/
optional_regexp() |
Takes a regular expression.
Any form fields that match the regexp specified are added to the list of optional fields.
:required_regexp => /name/
require_some() |
Takes a hash with each key pointing to an array.
The first field in the array is the number of fields that must be filled. The field is an array of fields to choose from. If the required number of fields are not found, the key name is reported in the list of missing fields.
:require_some => { :check_or_cc => [1, %w{cc_num check_no}] }
defaults() |
Takes a hash.
Fills in defaults but does not override required fields.
:defaults => { :country => "USA" }
dependencies() |
Takes a hash.
This hash which contains dependencies information. This is for the case where one optional fields has other requirements. The dependencies can be specified with an array. For example, if you enter your credit card number, the field cc_exp and cc_type should also be present. If the dependencies are specified with a hash then the additional constraint is added that the optional field must equal a key on the form for the dependencies to be added.
:dependencies => { :paytype => { :CC => [ :cc_type, :cc_exp ], :Check => :check_no }} :dependencies => { :street => [ :city, :state, :zipcode ] }
dependency_groups() |
Takes a hash pointing to an array.
If no fields are filled, then fine, but if any fields are filled, then all must be filled.
:dependency_groups => { :password_group => [ :pass1, :pass2 ] }
filters() |
Takes an array, symbol, or string.
Specified filters will be applied to ALL fields.
:filters => :strip
field_filters() |
Takes a hash.
Applies one or more filters to the specified field. See FormValidator::Filters for a list of builtin filters.
:field_filters => { :home_phone => :phone }
field_filter_regexp_map() |
Takes a regexp.
Applies one or more filters to fields matching regexp.
:field_filter_regexp_map => { /name/ => :capitalize }
untaint_all_constraints() |
Takes true.
If this is set, all fields which pass a constraint check are assigned the return value of the constraint check, and their values are untainted. This is overridden by untaint_constraint_fields.
:untaint_all_constraints => true
untaint_constraint_fields() |
Takes an array, symbol, or string.
Any field found in this array will be assigned the return value of the constraint check it passes, and it's value will be untainted.
:untaint_constraint_fields => %w{ name age }
constraint_regexp_map() |
Takes a hash.
Applies constraints to fields matching regexp and adds failed fields to the list of invalid fields. If untainting is enabled then the form element will be set to the result of the constraint method.
:constraint_regexp_map => { /code/ => :zip }
constraints() |
Takes a hash.
Apply constraint to each key and add failed fields to the invalid list. If untainting is enabled then the form element will be set to the result of the constraint method. Valid constraints can be one of the following:
Any constraint types listed below can be applied in series.
:fax => :american_phone
:age => /^1?\d{1,2}$/
:num => proc {|n| ((n % 2).zero?) ? n : nil}
# pass cc_no and cc_type in as arguments to cc_number constraint # and set {"cc_no" => ["cc_test"]} in failed hash if constraint fails. :cc_no => { :name => "cc_test", :constraint => :cc_number, :params => [:cc_no, :cc_type] } # If age coming in off the form is not all digits then set # {"age" => ["all_digits"]} in the failed hash. :age => { :name => "all_digits", :constraint => /^\d+$/ }
:constraints => { :age => /^1?\d{1,2}$/ } :constraints => { :zipcode => [:zip, /^\d+/], :fax => :american_phone, :email_addr => :email }