Forum: Ruby on Rails Selective Validations (feedback please)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4bd34a2216dc8bdbf1f017f64e4d59e8?d=identicon&s=25 kyle (Guest)
on 2005-11-23 22:33
(Received via mailing list)
# 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.
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-11-25 02:34
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.
Ef4c911f6f44b150b3739713a9962a4d?d=identicon&s=25 Cafe Babe (cafebabe)
on 2006-06-04 16:19
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 :(
This topic is locked and can not be replied to.