before_validation(*args, &block) public

Defines a callback that will get called right before validation.

class Person
  include ActiveModel::Validations
  include ActiveModel::Validations::Callbacks

  attr_accessor :name

  validates_length_of :name, maximum: 6

  before_validation :remove_whitespaces

  private
    def remove_whitespaces
      name.strip!
    end
end

person = Person.new
person.name = '  bob  '
person.valid? # => true
person.name   # => "bob"
Show source
Register or log in to add new notes.