Module: ActiveModel::Validations::ClassMethods
- Defined in:
- lib/validators/validates_datetime.rb,
lib/validators/validates_ip_address.rb,
lib/validators/validates_ownership_of.rb,
lib/validators/validates_cpf_format_of.rb,
lib/validators/validates_url_format_of.rb,
lib/validators/validates_cnpj_format_of.rb,
lib/validators/validates_email_format_of.rb
Instance Method Summary collapse
-
#validates_cnpj_format_of(*attr_names) ⇒ Object
(also: #validates_cnpj)
Validates whether or not the specified CNPJ is valid.
-
#validates_cpf_format_of(*attr_names) ⇒ Object
(also: #validates_cpf)
Validates whether or not the specified CPF is valid.
-
#validates_datetime(*attr_names) ⇒ Object
Validates whether or not the specified e-mail address is valid.
-
#validates_email_format_of(*attr_names) ⇒ Object
(also: #validates_email)
Validates whether or not the specified e-mail address is valid.
-
#validates_ip_address(*attr_names) ⇒ Object
Validates whether or not the specified URL is valid.
-
#validates_ownership_of(*attr_names) ⇒ Object
Validates whether the owner of the specified attribute is the same from the current object.
-
#validates_url_format_of(*attr_names) ⇒ Object
(also: #validates_url)
Validates whether or not the specified URL is valid.
Instance Method Details
#validates_cnpj_format_of(*attr_names) ⇒ Object Also known as: validates_cnpj
Validates whether or not the specified CNPJ is valid.
class User < ActiveRecord::Base
validates_cnpj_format_of :document
end
19 20 21 22 23 24 |
# File 'lib/validators/validates_cnpj_format_of.rb', line 19 def validates_cnpj_format_of(*attr_names) require "cnpj" validates_with CnpjValidator, _merge_attributes(attr_names) rescue LoadError fail "cpf_cnpj is not part of the bundle. Add it to Gemfile." end |
#validates_cpf_format_of(*attr_names) ⇒ Object Also known as: validates_cpf
Validates whether or not the specified CPF is valid.
class User < ActiveRecord::Base
validates_cpf_format_of :document
end
19 20 21 22 23 24 |
# File 'lib/validators/validates_cpf_format_of.rb', line 19 def validates_cpf_format_of(*attr_names) require "cpf" validates_with CpfValidator, _merge_attributes(attr_names) rescue LoadError fail "cpf_cnpj is not part of the bundle. Add it to Gemfile." end |
#validates_datetime(*attr_names) ⇒ Object
Validates whether or not the specified e-mail address is valid.
class User < ActiveRecord::Base
validates_datetime :birth
end
Other usages:
validates_datetime :starts_at, :after => 2.years.ago
validates_datetime :starts_at, :before => 2.years.ago
validates_datetime :starts_at, :before => :today
validates_datetime :starts_at, :before => :now
validates_datetime :starts_at, :before => :ends_at
validates_datetime :ends_at, :after => :starts_at
86 87 88 |
# File 'lib/validators/validates_datetime.rb', line 86 def validates_datetime(*attr_names) validates_with DatetimeValidator, _merge_attributes(attr_names) end |
#validates_email_format_of(*attr_names) ⇒ Object Also known as: validates_email
Validates whether or not the specified e-mail address is valid.
class User < ActiveRecord::Base
validates_email_format_of :email
end
21 22 23 |
# File 'lib/validators/validates_email_format_of.rb', line 21 def validates_email_format_of(*attr_names) validates_with EmailValidator, _merge_attributes(attr_names) end |
#validates_ip_address(*attr_names) ⇒ Object
Validates whether or not the specified URL is valid.
validates_ip_address :ip #=> accepts both v4 and v6
validates_ip_address :ip, :only => :v4
validates_ip_address :ip, :only => :v6
42 43 44 |
# File 'lib/validators/validates_ip_address.rb', line 42 def validates_ip_address(*attr_names) validates_with IpAddressValidator, _merge_attributes(attr_names) end |
#validates_ownership_of(*attr_names) ⇒ Object
Validates whether the owner of the specified attribute is the same from the current object.
class Task < ActiveRecord::Base
belongs_to :user
belongs_to :category
validates_ownership_of :category, :with => :user
end
user = User.find(1)
another_user = User.find(2)
user_category = user.categories.first
another_user_category = another_user.categories.first
task = user.tasks.create(:category => user_category)
task.valid?
#=> true
task = user.tasks.create(:category => another_user_category)
task.valid?
#=> false
46 47 48 |
# File 'lib/validators/validates_ownership_of.rb', line 46 def validates_ownership_of(*attr_names) validates_with OwnershipValidator, _merge_attributes(attr_names) end |
#validates_url_format_of(*attr_names) ⇒ Object Also known as: validates_url
Validates whether or not the specified URL is valid.
class User < ActiveRecord::Base
validates_url_format_of :site
end
21 22 23 |
# File 'lib/validators/validates_url_format_of.rb', line 21 def validates_url_format_of(*attr_names) validates_with UrlValidator, _merge_attributes(attr_names) end |