validatable_instance_methods.rb

Path: lib/data_mapper/validatable_extensions/validatable_instance_methods.rb
Last Update: Thu Oct 22 18:04:20 +0000 2009

Validations

DataMapper uses the ‘Validatable’ gem to validate models.

Example:

  class Person < DataMapper::Base
    property :name, :string
    property :email, :string
    property :password, :string

    validates_presence_of :name, :email
    validates_length_of :password, :minimum => 6, :on => :create
    validates_format_of :email, :with => :email_address, :message => 'Please provide a valid email address.'
  end

  p = Person.new
  p.valid? #=> false
  p.errors.full_messages #=> ["Email must not be blank", "Please provide a valid email address.", "Name must not be blank"]

  p.save #=> false
  p.errors.full_messages #=> ["Password must be more than 5 characters long", "Email must not be blank",
                                 "Please provide a valid email address.", "Name must not be blank"]

[Validate]