~ubuntu-branches/ubuntu/saucy/ruby-validatable/saucy

1 by Cédric Boutillier
Import upstream version 1.6.7
1
= Validatable
2
3
Validatable is a library for adding validations.
4
5
by Jay[http://jayfields.blogspot.com] Fields[http://jayfields.blogspot.com]
6
7
== Download and Installation
8
9
You can download Validatable from here[http://rubyforge.org/projects/validatable] or install it with the following command.
10
11
 $ gem install validatable
12
13
== License
14
 	
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).
16
17
== Examples
18
19
Validation of an entire hierarchy of objects with errors aggregated at the root object.
20
21
	class Person
22
	  include Validatable
23
	  validates_presence_of :name
24
	  attr_accessor :name
25
	end
26
27
	class PersonPresenter
28
	  include Validatable
29
	  include_validations_for :person
30
	  attr_accessor :person
31
  
32
	  def initialize(person)
33
	    @person = person
34
	  end
35
	end
36
37
	presenter = PersonPresenter.new(Person.new)
38
	presenter.valid? #=> false
39
	presenter.errors.on(:name) #=> "can't be blank"
40
41
Validations that turn off after X times of failed attempts.
42
43
	class Person
44
	  include Validatable
45
	  validates_presence_of :name, :times => 1
46
	  attr_accessor :name
47
	end
48
49
	person = Person.new
50
	person.valid? #=> false
51
	person.valid? #=> true
52
53
Validations can be given levels. If a validation fails on a level the validations for subsequent levels will not be executed.
54
55
	class Person
56
	  include Validatable
57
	  validates_presence_of :name, :level => 1, :message => "name message"
58
	  validates_presence_of :address, :level => 2
59
	  attr_accessor :name, :address
60
	end
61
62
	person = Person.new
63
	person.valid? #=> false
64
	person.errors.on(:name) #=> "name message"
65
	person.errors.on(:address) #=> nil
66
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.
68
69
  class MortgageApplication
70
    include Validatable
71
    validates_presence_of :ssn, :groups => [:saving, :underwriting]
72
    validates_presence_of :name, :groups => :underwriting
73
    attr_accessor :name, :ssn
74
  end
75
  
76
  application = MortgageApplication.new
77
  application.ssn = 377990118
78
  application.valid_for_saving? #=> true
79
  application.valid_for_underwriting? #=> false
80
  
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.
82
83
Similar to Rails, Validatable also supports conditional validation.
84
85
	class Person
86
	  include Validatable
87
	  attr_accessor :name
88
	  validates_format_of :name, :with => /.+/, :if => Proc.new { !name.nil? }
89
	end
90
	Person.new.valid? #=> true
91
		
92
Validatable also exposes an after_validate hook method.
93
94
	class Person
95
	  include Validatable
96
	  validates_presence_of :name
97
	  attr_accessor :name
98
	end
99
	
100
	class ValidatesPresenceOf
101
	  after_validate do |result, instance, attribute|
102
			instance.errors.add("#{attribute} can't be blank") unless result
103
		end
104
	end
105
106
	person = Person.new
107
	person.valid? #=> false
108
	person.errors.on(:name) #=> "name can't be blank"
109
	
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
112
113
In the above example the attribute "name" is appended to the message.
114
115
See the tests for more examples
116
117
== Contributors
118
Rick Bradley, Anonymous Z, Jason Miller, Ali Aghareza, Xavier Shay, Dan Manges, Karthik Krishnan and Venkat, Clint Bishop