Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 116 lines (84 sloc) 3.629 kb
34a9faf creating version 1.0
jaycfields authored
1 = Validatable
2
3 Validatable is a library for adding validations.
4
13f3ac7 John Nunemaker Prepping for gem name move from validatable to jnunemaker-validatable
authored
5 Created by Jay Fields, updated by John Nunemaker
34a9faf creating version 1.0
jaycfields authored
6
13f3ac7 John Nunemaker Prepping for gem name move from validatable to jnunemaker-validatable
authored
7 == Installation
8
9 $ gem install jnunemaker-validatable -s http://gemcutter.org
34a9faf creating version 1.0
jaycfields authored
10
11 == License
12
13 You may use, copy and redistribute this library under the same terms as Ruby itself (see http://www.ruby-lang.org/en/LICENSE.txt).
14
15 == Examples
16
74f4cdb better docs, removed pkg folder
jaycfields authored
17 Validation of an entire hierarchy of objects with errors aggregated at the root object.
34a9faf creating version 1.0
jaycfields authored
18
74f4cdb better docs, removed pkg folder
jaycfields authored
19 class Person
20 include Validatable
21 validates_presence_of :name
22 attr_accessor :name
23 end
34a9faf creating version 1.0
jaycfields authored
24
74f4cdb better docs, removed pkg folder
jaycfields authored
25 class PersonPresenter
26 include Validatable
27 include_validations_for :person
28 attr_accessor :person
29
30 def initialize(person)
31 @person = person
32 end
33 end
93c3c52 added support for :if conditional
jaycfields authored
34
74f4cdb better docs, removed pkg folder
jaycfields authored
35 presenter = PersonPresenter.new(Person.new)
36 presenter.valid? #=> false
37 presenter.errors.on(:name) #=> "can't be blank"
93c3c52 added support for :if conditional
jaycfields authored
38
74f4cdb better docs, removed pkg folder
jaycfields authored
39 Validations that turn off after X times of failed attempts.
40
41 class Person
42 include Validatable
43 validates_presence_of :name, :times => 1
44 attr_accessor :name
45 end
46
47 person = Person.new
48 person.valid? #=> false
49 person.valid? #=> true
50
51 Validations can be given levels. If a validation fails on a level the validations for subsequent levels will not be executed.
52
53 class Person
54 include Validatable
55 validates_presence_of :name, :level => 1, :message => "name message"
56 validates_presence_of :address, :level => 2
57 attr_accessor :name, :address
58 end
59
60 person = Person.new
61 person.valid? #=> false
62 person.errors.on(:name) #=> "name message"
63 person.errors.on(:address) #=> nil
64
ec33f8c package for 1.2.2
jaycfields authored
65 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.
66
67 class MortgageApplication
68 include Validatable
69 validates_presence_of :ssn, :groups => [:saving, :underwriting]
70 validates_presence_of :name, :groups => :underwriting
71 attr_accessor :name, :ssn
72 end
73
74 application = MortgageApplication.new
75 application.ssn = 377990118
76 application.valid_for_saving? #=> true
77 application.valid_for_underwriting? #=> false
6993e59 small rdoc changes and applying understandable patch from Xavier Shay
jaycfields authored
78
79 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.
ec33f8c package for 1.2.2
jaycfields authored
80
74f4cdb better docs, removed pkg folder
jaycfields authored
81 Similar to Rails, Validatable also supports conditional validation.
82
83 class Person
84 include Validatable
85 attr_accessor :name
86 validates_format_of :name, :with => /.+/, :if => Proc.new { !name.nil? }
87 end
88 Person.new.valid? #=> true
75ba177 adding rdoc for recent changes
jaycfields authored
89
90 Validatable also exposes an after_validate hook method.
91
92 class Person
93 include Validatable
94 validates_presence_of :name
95 attr_accessor :name
96 end
97
98 class ValidatesPresenceOf
99 after_validate do |result, instance, attribute|
100 instance.errors.add("#{attribute} can't be blank") unless result
101 end
102 end
103
104 person = Person.new
105 person.valid? #=> false
106 person.errors.on(:name) #=> "name can't be blank"
107
108 The after_validate hook yields the result of the validation being run,
6993e59 small rdoc changes and applying understandable patch from Xavier Shay
jaycfields authored
109 the instance the validation was run on, and the attribute that was validated
75ba177 adding rdoc for recent changes
jaycfields authored
110
111 In the above example the attribute "name" is appended to the message.
34a9faf creating version 1.0
jaycfields authored
112
11212de gem for rick's change
jaycfields authored
113 See the tests for more examples
114
115 == Contributors
f449963 adding Yi and Chris as contributors
jaycfields authored
116 Rick Bradley, Anonymous Z, Jason Miller, Ali Aghareza, Xavier Shay, Dan Manges, Karthik Krishnan and Venkat, Clint Bishop, Chris Didyk, Yi Wen
Something went wrong with that request. Please try again.