module ActiveModel::Validations::ClassMethods
def validates(*attributes)
and +:message+ can be given to one specific validator, as a hash:
Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+, +:strict+
validates :token, length: { is: 24 }, strict: TokenLengthException
validates :password, presence: true, confirmation: true, if: :password_required?
Example:
:strict option can also be set to any other exception.
will raise ActiveModel::StrictValidationFailed instead of adding the error.
* :strict - If the :strict option is set to true
* :allow_blank - Skip validation if the attribute is blank.
* :allow_nil - Skip validation if the attribute is +nil+.
+false+ value.
method, proc, or string should return or evaluate to a +true+ or
or unless: Proc.new { |user| user.signup_step <= 2 }). The
if the validation should not occur (e.g. unless: :skip_validation,
* :unless - Specifies a method, proc, or string to call to determine
proc or string should return or evaluate to a +true+ or +false+ value.
or if: Proc.new { |user| user.signup_step > 2 }). The method,
if the validation should occur (e.g. if: :allow_validation,
* :if - Specifies a method, proc, or string to call to determine
on: [:create, :custom_validation_context])
on: :custom_validation_context or
or an array of symbols. (e.g. on: :create or
Runs in all validation contexts by default +nil+. You can pass a symbol
* :on - Specifies the contexts where this validation is active.
There is also a list of options that could be used along with validators:
including regular expressions and strings are passed as options[:with].
validator's initializer as options[:in] while other types
When using shortcut form, ranges and arrays are passed to your
validates :password, length: 6..20
validates :role, inclusion: %w(admin contributor)
validates :email, format: /@/
and strings in shortcut form.
The validators hash can also handle regular expressions, ranges, arrays
validates :name, :'film/title' => true
used within any class.
Additionally validator classes may be in another namespace and still
end
validates :name, title: true
end
end
record.errors.add attribute, "must start with 'the'" unless /\Athe/i.match?(value)
def validate_each(record, attribute, value)
class TitleValidator < ActiveModel::EachValidator
include ActiveModel::Validations
class Film
allowing custom modules of validators to be included as needed.
Validator classes may also exist within the class being validated
end
validates :email, presence: true, email: true
validates :name, presence: true, length: { maximum: 100 }
attr_accessor :name, :email
include ActiveModel::Validations
class Person
end
end
/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i.match?(value)
record.errors.add attribute, (options[:message] || "is not an email") unless
def validate_each(record, attribute, value)
class EmailValidator < ActiveModel::EachValidator
and default validators in one call for a given attribute.
The power of the +validates+ method comes when using custom validators
validates :username, presence: true
validates :age, numericality: true
validates :first_name, length: { maximum: 30 }
validates :age, inclusion: { in: 0..9 }
validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create }
validates :username, exclusion: { in: %w(admin superuser) }
validates :password, confirmation: true
validates :terms, acceptance: true
validates :username, absence: true
Examples of using the default rails validators:
custom validator classes in their place such as PresenceValidator.
validators can be overridden inside specific classes by creating
validator classes ending in 'Validator'. Note that Rails default
This method is a shortcut to all default validators and any custom
def validates(*attributes) defaults = attributes.extract_options!.dup validations = defaults.slice!(*_validates_default_keys) raise ArgumentError, "You need to supply at least one attribute" if attributes.empty? raise ArgumentError, "You need to supply at least one validation" if validations.empty? defaults[:attributes] = attributes validations.each do |key, options| key = "#{key.to_s.camelize}Validator" begin validator = key.include?("::") ? key.constantize : const_get(key) rescue NameError raise ArgumentError, "Unknown validator: '#{key}'" end next unless options validates_with(validator, defaults.merge(_parse_validates_options(options))) end end