[ rails ] ApplicationRecord Validation

class Person < ApplicationRecord
validates :name, presence: true
end
Person.create(name: "John Doe").valid? # => true
Person.create(name: nil).valid? # => false

Validation

The following methods trigger validations, and will save the object to the database only if the object is valid:

create
create!
save
save!
update
update!

The bang versions (e.g. save!) raise an exception if the record is invalid. The non-bang versions don’t: save and update return false, and create just returns the object.

Skip Validation

The following methods skip validations, and will save the object to the database regardless of its validity. They should be used with caution.

decrement!
decrement_counter
increment!
increment_counter
toggle!
touch
update_all
update_attribute
update_column
update_columns
update_counters

Note that save also has the ability to skip validations if passed validate:
false as an argument. This technique should be used with caution.

save(validate: false)

errors ( ActiveModel::Errors )

class Person < ApplicationRecord
validates :name, presence: true
end
>> Person.new.errors[:name].any? # => false
>> Person.create.errors[:name].any? # => true

errors.detail

class Person < ApplicationRecord
validates :name, presence: true
end
>> person = Person.new
>> person.valid?
>> person.errors.details[:name] # => [{error: :blank}]

on: :create

# it will be possible to update email with a duplicated value
validates :email, uniqueness: true, on: :create

Validate Helper

validates :name, presence: true

file_validators gem

validates :avatar, file_size: { less_than_or_equal_to: 2.megabytes }

Custom Validator

Must override the validate_each(record, attribute, value) method

email_method_validator.rb

class EmailMethodValidator < ActiveModel::EachValidator
    def validate_each(*args)

        record, attribute, value = *args
        unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
             record.errors[attribute] << (options[:message] || "is not an email")
        end
    end
end

class Person < ApplicationRecord
     validates :email, presence: true, email_method: true
end

The ActiveRecord can be retrieved by using record.

For example, record.class, record[attribute]

ActiveModel::Errors method

Syntax

add(attribute, message = :invalid, options = {})

attribute should be set to :base if the error is not directly associated with a single attribute.

person.errors.add(:base, :name_or_email_blank)

NOTE : :name_or_email_blank will be in i18n.

ref : http://guides.rubyonrails.org/active_record_validations.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s