3
Validatable is a library for adding validations.
5
by Jay[http://jayfields.blogspot.com] Fields[http://jayfields.blogspot.com]
7
== Download and Installation
9
You can download Validatable from here[http://rubyforge.org/projects/validatable] or install it with the following command.
11
$ gem install validatable
15
You may use, copy and redistribute this library under the same terms as Ruby itself (see http://www.ruby-lang.org/en/LICENSE.txt).
19
Validation of an entire hierarchy of objects with errors aggregated at the root object.
23
validates_presence_of :name
29
include_validations_for :person
32
def initialize(person)
37
presenter = PersonPresenter.new(Person.new)
38
presenter.valid? #=> false
39
presenter.errors.on(:name) #=> "can't be blank"
41
Validations that turn off after X times of failed attempts.
45
validates_presence_of :name, :times => 1
50
person.valid? #=> false
51
person.valid? #=> true
53
Validations can be given levels. If a validation fails on a level the validations for subsequent levels will not be executed.
57
validates_presence_of :name, :level => 1, :message => "name message"
58
validates_presence_of :address, :level => 2
59
attr_accessor :name, :address
63
person.valid? #=> false
64
person.errors.on(:name) #=> "name message"
65
person.errors.on(:address) #=> nil
67
Validations can also be given groups. Groups can be used to validate an object when it can be valid in various states. For example a mortgage application may be valid for saving (saving a partial application), but that same mortgage application would not be valid for underwriting. In our example a application can be saved as long as a Social Security Number is present; however, an application can not be underwritten unless the name attribute contains a value.
69
class MortgageApplication
71
validates_presence_of :ssn, :groups => [:saving, :underwriting]
72
validates_presence_of :name, :groups => :underwriting
73
attr_accessor :name, :ssn
76
application = MortgageApplication.new
77
application.ssn = 377990118
78
application.valid_for_saving? #=> true
79
application.valid_for_underwriting? #=> false
81
As you can see, you can use an array if the validation needs to be part of various groups. However, if the validation only applies to one group you can simply use a symbol for the group name.
83
Similar to Rails, Validatable also supports conditional validation.
88
validates_format_of :name, :with => /.+/, :if => Proc.new { !name.nil? }
90
Person.new.valid? #=> true
92
Validatable also exposes an after_validate hook method.
96
validates_presence_of :name
100
class ValidatesPresenceOf
101
after_validate do |result, instance, attribute|
102
instance.errors.add("#{attribute} can't be blank") unless result
107
person.valid? #=> false
108
person.errors.on(:name) #=> "name can't be blank"
110
The after_validate hook yields the result of the validation being run,
111
the instance the validation was run on, and the attribute that was validated
113
In the above example the attribute "name" is appended to the message.
115
See the tests for more examples
118
Rick Bradley, Anonymous Z, Jason Miller, Ali Aghareza, Xavier Shay, Dan Manges, Karthik Krishnan and Venkat, Clint Bishop
b'\\ No newline at end of file'