class PersonValidatable currently supports
include Validatable
attr_accessor :name
validates_presence_of :name
end
person = Person.new
person.valid? #=> false
person.errors.on(:name) #=> "can't be empty"
- validates_presence_of
- validates_length_of
- validates_format_of
- validates_confirmation_of
- validates_acceptance_of
Validation of an entire hierarchy of objects with errors aggregated at the root object.
class PersonValidations that turn off after X times of failed attempts.
include Validatable
validates_presence_of :name
attr_accessor :name
end
class PersonPresenter
include Validatable
include_validations_for :person
attr_accessor :person
def initialize(person)
@person = person
end
end
presenter = PersonPresenter.new(Person.new)
presenter.valid? #=> false
presenter.errors.on(:name) #=> "can't be blank"
class PersonValidations can be given levels. If a validation fails on a level the validations for subsequent levels will not be executed.
include Validatable
validates_presence_of :name, :times => 1
attr_accessor :name
end
person = Person.new
person.valid? #=> false
person.valid? #=> true
class PersonSimilar to Rails, Validatable also supports conditional validation.
include Validatable
validates_presence_of :name, :level => 1, :message => "name message"
validates_presence_of :address, :level => 2
attr_accessor :name, :address
end
person = Person.new
person.valid? #=> false
person.errors.on(:name) #=> "name message"
person.errors.on(:address) #=> nil
class Person
include Validatable
attr_accessor :name
validates_format_of :name, :with => /.+/, :if => Proc.new { !name.nil? }
end
Person.new.valid? #=> true
Hey Jay!
ReplyDeleteBeen to validatable.rubyforge.org, installed it but seems there is smthg not working!
Hello
ReplyDeleteThanks for letting me know. Can you be more specific about what's not working?
Cheers, Jay
Hello Jay,
ReplyDeleteis it compatible with ActiveRecord's validations ?
Hello Stevatil,
ReplyDeleteIt's not meant to replace or add to AR validations. It's meant to add the same validation functionality to any object.
Cheers, Jay
Hm. Installed it, but where did it go?
ReplyDelete% gem list validatable
*** LOCAL GEMS ***
validatable (1.5.2)
Validatable is a library for adding validations.
% irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):005:0> gem 'validatable'
Gem::LoadError: Could not find RubyGem validatable (>= 0.0.0)
...
irb(main):003:0> require 'validatable'
LoadError: no such file to load -- validatable
...
kwerle@pobox.com
My fault. Was mixing ruby & jruby - which is fine, as long as you remember jruby's gem command is also gem, so you must specify the right path.
ReplyDelete