validates_each
validates_each(*attr_names, &block)
public
Validates each attribute against a block.
class Person include ActiveModel::Validations attr_accessor :first_name, :last_name validates_each :first_name, :last_name, allow_blank: true do |record, attr, value| record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z end end
Options:
-
:on - Specifies the contexts where this validation is active. You can pass a symbol or an array of symbols. (e.g. on: :create or on: :custom_validation_context or on: [:create, :custom_validation_context])
-
:allow_nil - Skip validation if attribute is nil.
-
:allow_blank - Skip validation if attribute is blank.
-
:if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. if: :allow_validation, or if: Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.
-
:unless - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. unless: :skip_validation, or unless: Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.
Re: Validate an optional URL field
Actually it’s easier to use validates_format_of for this task. Please refer to the comments under the doc.
Validate Mixup
Looks like the docs from validate got mixed up here. Only the last example is actually relevant to validates_each.
Validate an optional URL field
Let’s say that you have an optional URL field to one of your models and you want to validate the URL. You can accomplish this by using the URI library:
require 'uri' # Put this at the beginning of your model file validates_each :url, :allow_blank => true do |record, field, value| begin valid = (URI.parse(value).scheme =~ /https?/) rescue URI::InvalidURIError valid = false end record.errors.add field, "not a valid url" unless valid end
If you want to add even more testing in there, just go ahead. For now, we just check that the link is to a HTTP resource, but you might have other requirements. This will allow stuff like “http://example” since “example” might be a valid intranet domain. If you want to check for a TLD in there, you can do so with a simple regexp.
For more information about the URI library, check out http://apidock.com/ruby/URI/