Selective Validations (feedback please)


#1

I’m mostly done with a simple plugin that lets you apply validations

in named groups. This is expecially useful for multipage forms or
wizards. I thought I’d put the idea up for feedback. Please let me
know what you think!

Sample code below…

class Sample < ActiveRecord::Base
validation_group “v1” do
validates_presence_of :field
validates_numericality_of :other_field
end

#Validation groups can overlap, and have different aliases
named_validation [“v2”, “size_validations”] { validates_size_of
:field, :in => 0…9 }
named_validation [“v3”, “size_validations”] { validates_size_of
:some_field, :in => 0…9 }

end

All validations enabled by default.

Sample.disable_validations [“v2”, “v3”]
Sample.disable_only_validations [“v2”, “v3”] #Enable all, then disable
specified
Sample.enable_validations “v1”
Sample.enable_only_validations “v3” #Disable all, then enable specified
Sample.enable_validations #Enable all

Now tell me what you think.


#2

I’ve been looking for this!

I need to check if an object is valid, except for one field, which won’t
be valid until I do some processing. But I don’t want to do that
processing unless the object is already valid otherwise. Right now,
there’s no simple way to do this check.

Nice work (at least, looking at the interface…).

I would like something like
obj.valid_excluding_group_x?

kyle wrote:

I’m mostly done with a simple plugin that lets you apply validations

in named groups. This is expecially useful for multipage forms or
wizards. I thought I’d put the idea up for feedback. Please let me
know what you think!

Sample code below…

class Sample < ActiveRecord::Base
validation_group “v1” do
validates_presence_of :field
validates_numericality_of :other_field
end

#Validation groups can overlap, and have different aliases
named_validation [“v2”, “size_validations”] { validates_size_of
:field, :in => 0…9 }
named_validation [“v3”, “size_validations”] { validates_size_of
:some_field, :in => 0…9 }

end

All validations enabled by default.

Sample.disable_validations [“v2”, “v3”]
Sample.disable_only_validations [“v2”, “v3”] #Enable all, then disable
specified
Sample.enable_validations “v1”
Sample.enable_only_validations “v3” #Disable all, then enable specified
Sample.enable_validations #Enable all

Now tell me what you think.


#3

Great, it’s very interesting, but where can I get this plugin? Is it
open source?
Now I am writing something like…

validates_presence_of :name, :if => Proc.new { |user|
user.method=:sign_up}

def self.sign_up
user=User.new…
user.method=:sign_up
user.save
end

It is ugly and not flexible :frowning: