Module: Fend::Plugins::ValidationHelpers
- Defined in:
- lib/fend/plugins/validation_helpers.rb
Overview
‘validation_helpers` plugin provides additional `Param` methods for common validation cases.
plugin :validation_helpers
validate do |i|
i.params(:username) do |username|
username.validate_presence
username.validate_max_length(20)
username.validate_type(String)
end
end
You can find list of all available helpers in ParamMethods.
## Overriding default messages
You can override default messages by specifying ‘:default_messages` options when loading the plugin
plugin :validation_helpers, default_messages: {
exact_length: ->(length) { I18n.t("errors.exact_length", length: length) },
presence: "cannot be blank",
type: ->(type) { "is not of valid type. Must be #{type.to_s.downcase}" }
}
Custom messages can be defined by passing ‘:message` option to validation helper method:
username.validate_max_length(20, message: "must be shorter than 20 chars")
Defined Under Namespace
Modules: ParamClassMethods, ParamMethods
Constant Summary collapse
- DEFAULT_MESSAGES =
{ absence: -> { "must be absent" }, acceptance: -> { "must be accepted" }, equality: ->(value) { "must be equal to '#{value}'" }, exact_length: ->(length) { "length must be equal to #{length}" }, exclusion: ->(list) { "cannot be one of: #{list.join(', ')}" }, format: -> { "is in invalid format" }, greater_than: ->(value) { "must be greater than #{value}" }, greater_than_or_equal_to: ->(value) { "must be greater than or equal to #{value}" }, inclusion: ->(list) { "must be one of: #{list.join(', ')}" }, length_range: ->(range) { "length must be between #{range.min} and #{range.max}" }, less_than: ->(value) { "must be less than #{value}" }, less_than_or_equal_to: ->(value) { "must be less than or equal to #{value}" }, max_length: ->(value) { "length cannot be greater than #{value}" }, min_length: ->(value) { "length cannot be less than #{value}" }, presence: -> { "must be present" }, type: ->(type) { "must be #{type.to_s.downcase}" } }.freeze
- ACCEPTABLE =
[1, "1", true, "true", "TRUE", :yes, "YES", "yes"].freeze
- UNSUPPORTED_TYPE =
"__unsupported_type__".freeze
Class Method Summary collapse
- .configure(validation, opts = {}) ⇒ Object
-
.load_dependencies(validation, *args, &block) ⇒ Object
depends on ValueHelpers plugin, which provides methods that are used in certain validation helpers.
Class Method Details
.configure(validation, opts = {}) ⇒ Object
43 44 45 |
# File 'lib/fend/plugins/validation_helpers.rb', line 43 def self.configure(validation, opts = {}) validation.opts[:validation_default_messages] = (validation.opts[:validation_default_messages] || {}).merge(opts[:default_messages] || {}) end |
.load_dependencies(validation, *args, &block) ⇒ Object
depends on ValueHelpers plugin, which provides methods that are used in certain validation helpers
39 40 41 |
# File 'lib/fend/plugins/validation_helpers.rb', line 39 def self.load_dependencies(validation, *args, &block) validation.plugin(:value_helpers) end |