separate validations

0 views
Skip to first unread message

Bahador Bahadori

unread,
Jul 7, 2008, 12:04:32 AM7/7/08
to malay...@googlegroups.com
Hello,
I want to have separate validations, some in my first page, and two validations for only one variable in the next page..
but all the validations run in the very first page...
any idea how to do this?
thanks

Kamal Fariz

unread,
Jul 7, 2008, 12:43:44 AM7/7/08
to malay...@googlegroups.com
IMHO, there isn't a native way to selectively call certain validations
with ActiveRecord. DataMapper on the other hand allows you to set
contexts which would be perfect to your problem.

Someone wrote a plugin for grouped validation but I haven't used it
myself http://alexkira.blogspot.com/2007/09/rails-validation-using-validation.html

Bahador Bahadori

unread,
Jul 7, 2008, 2:14:59 AM7/7/08
to malay...@googlegroups.com
it looks nice, but i cant get to install it.. it says its already exists, but when i try to use its methods it throws exceptions...
how about if i try to use  attr_accessor to override my variable... or just use javascript to validate presence and format of that particular variable?

Pengzhi

unread,
Jul 9, 2008, 12:04:04 AM7/9/08
to Malaysia Ruby Brigade
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

Bahador Bahadori

unread,
Jul 9, 2008, 2:41:55 AM7/9/08
to malay...@googlegroups.com
yeah as u said its not quite the rails way... i'm just gonna use javascript to validate that fied.. the rails core team should do sth for validating separate pages in their next release! :D thanks anyways

alex...@gmail.com

unread,
Jul 12, 2008, 6:50:53 PM7/12/08
to Malaysia Ruby Brigade
Hi,

It is my plugin.. At what point is it saying that it already exists
(when trying to install the plugin, or use the methods?) Also, what
version of rails are you using? If I can get a bit of info i can look
into it..

Thanks,

Alex

Bahador Bahadori

unread,
Jul 13, 2008, 10:26:06 PM7/13/08
to malay...@googlegroups.com
I was getting an error saying some of the files exist.. but when i tried to download the plugin just now it worked liked charm!
Although i'm still having problem calling the "@user.enable_validation_group :step1" from my controller,it doesn't go through the @user.valid? to validate the step1 group, it validates all of them at same time
Cheers
Reply all
Reply to author
Forward
0 new messages