module Fend::Plugins::ValidationHelpers::ParamMethods
Public Instance Methods
Validates that param value is blank. To see what values are considered as blank, check ValueHelpers::ParamMethods#blank?
.
id.validate_absence
# File lib/fend/plugins/validation_helpers.rb, line 80 def validate_absence(opts = {}) add_error(:absence, opts[:message]) if present? end
Validates acceptance. Potential use case would be checking if Terms of Service has been accepted.
By default, validation will pass if value is one of: `[1, “1”, :true, true, “true”, “TRUE”, :yes, “YES”, “yes”]`
You can pass the `:as` option with custom list of acceptable values:
tos.validate_acceptance(as: ["Agreed", "OK"])
# File lib/fend/plugins/validation_helpers.rb, line 93 def validate_acceptance(opts = {}) as = Array(opts.fetch(:as, ACCEPTABLE)) add_error(:acceptance, opts[:message]) unless as.include?(value) end
Validates that param value is equal to the specified value.
color.validate_equality("black")
# File lib/fend/plugins/validation_helpers.rb, line 102 def validate_equality(rhs, opts = {}) add_error(:equality, opts[:message], rhs) unless value.eql?(rhs) end
Validates that param value length is equal to the specified value. Works with any object that responds to `#length` method.
code.validate_exact_length(10)
# File lib/fend/plugins/validation_helpers.rb, line 110 def validate_exact_length(exact_length, opts = {}) value_length = value.respond_to?(:length) ? value.length : UNSUPPORTED_TYPE return if !value_length.eql?(UNSUPPORTED_TYPE) && value_length.eql?(exact_length) add_error(:exact_length, opts[:message], exact_length) end
Validates that param value is not one of the specified values.
account_type.validate_exclusion(["admin", "editor"])
# File lib/fend/plugins/validation_helpers.rb, line 121 def validate_exclusion(exclude_from, opts = {}) add_error(:exclusion, opts[:message], exclude_from) if exclude_from.include?(value) end
Validates that param value is a match for specified regex.
name.validate_format(/\A[a-z]\z/i)
# File lib/fend/plugins/validation_helpers.rb, line 128 def validate_format(format, opts = {}) add_error(:format, opts[:message]) if format.match(value.to_s).nil? end
Validates that param value is greater than specified value
age.validate_greater_than(18)
# File lib/fend/plugins/validation_helpers.rb, line 135 def validate_greater_than(rhs, opts = {}) add_error(:greater_than, opts[:message], rhs) unless value.is_a?(Numeric) && value > rhs end
Validates that param value is greater than or equal to specified value
age.validate_greater_than_or_equal_to(18)
Aliased as `validate_gteq`
age.validate_gteq(10)
# File lib/fend/plugins/validation_helpers.rb, line 146 def validate_greater_than_or_equal_to(rhs, opts = {}) add_error(:greater_than_or_equal_to, opts[:message], rhs) unless value.is_a?(Numeric) && value >= rhs end
Validates that param value is one of the specified values.
account_type.validate_inclusion(["admin", "editor"])
# File lib/fend/plugins/validation_helpers.rb, line 154 def validate_inclusion(include_in, opts = {}) add_error(:inclusion, opts[:message], include_in) unless include_in.include?(value) end
Validates that param value length is within specified range
code.validate_length_range(10..15)
# File lib/fend/plugins/validation_helpers.rb, line 161 def validate_length_range(range, opts = {}) value_length = value.respond_to?(:length) ? value.length : UNSUPPORTED_TYPE return if !value_length.eql?(UNSUPPORTED_TYPE) && range.include?(value_length) add_error(:length_range, opts[:message], range) end
Validates that param value is less than specified value
funds.validate_less_than(100)
# File lib/fend/plugins/validation_helpers.rb, line 172 def validate_less_than(rhs, opts = {}) add_error(:less_than, opts[:message], rhs) unless value.is_a?(Numeric) && value < rhs end
Validates that param value is less than or equal to specified value
funds.validate_less_than_or_equal_to(100)
Aliased as `validate_lteq`
funds.validate_lteq(100)
# File lib/fend/plugins/validation_helpers.rb, line 183 def validate_less_than_or_equal_to(rhs, opts = {}) add_error(:less_than_or_equal_to, opts[:message], rhs) unless value.is_a?(Numeric) && value <= rhs end
Validates that param value length is not greater than specified value
password.validate_max_length(15)
# File lib/fend/plugins/validation_helpers.rb, line 191 def validate_max_length(length, opts = {}) value_length = value.respond_to?(:length) ? value.length : UNSUPPORTED_TYPE return if !value_length.eql?(UNSUPPORTED_TYPE) && value_length <= length add_error(:max_length, opts[:message], length) end
Validates that param value length is not less than specified value
password.validate_min_length(5)
# File lib/fend/plugins/validation_helpers.rb, line 202 def validate_min_length(length, opts = {}) value_length = value.respond_to?(:length) ? value.length : UNSUPPORTED_TYPE return if !value_length.eql?(UNSUPPORTED_TYPE) && value_length >= length add_error(:min_length, opts[:message], length) end
Validates that param value is present. To see what values are considered as present, check ValueHelpers::ParamMethods#present?
name.validate_presence
# File lib/fend/plugins/validation_helpers.rb, line 214 def validate_presence(opts = {}) add_error(:presence, opts[:message]) if blank? end
Uses ValueHelpers::ParamMethods#type_of?
method to validate that param value is of specified type.
tags.validate_type(Array)
# File lib/fend/plugins/validation_helpers.rb, line 222 def validate_type(type, opts = {}) add_error(:type, opts[:message], type) unless type_of?(type) end