hmm...there are 2 ways to do this, one for built-in validations,
another for custom validations. Also this assume page number is stored
in a column named 'page':
class Content < ActiveRecord::Base
# built-in validations
with_options :if => Proc.new { |a|
a.page == 1 } do |this|
# when it is page 1 do this validations
this.validates_presence_of :text
this.validates_numericality_of :reference
end
with_options :if => Proc.new { |a|
a.page == 2 } do |this|
# when it is page 2 do this validations
# bla bla
end
# custom validations
def validate
case
self.page
when 1
self.errors.add('text', 'Can only contain text1') if /(\d
+)/.match(self.text)
when 2
self.errors.add('text', 'Can only contain text2') if /(\d
+)/.match(self.text)
else
#buggy
#ActiveRecord::Base.validates_presence_of :text
self.errors.add('text', 'Can only contain text3') if /(\d
+)/.match(self.text)
end
end
end
You might realize that this is not very dry, repeating validations for
page 1, 2, etc. Also, why didn't you add built in validations in
'validate' method, you might ask, well, I did, and it's buggy. Try
putting something like 'ActiveRecord::Base.validates_numericality_of '
and you will see what i mean. If you found a better solution please
let me know, haven't really dug deep enough into validations to
understand it.
Pengzhi