Validation in a specific form only

Is there a way to initiate validation only in certain forms (controller action), and not globally with every save or update? Something like a flag User.create(:validate=>true).

+5
source share
3 answers

Yes, you can provide conventions for verification, for example:

validates_presence_of :something, :if => :special?

private

def make_sepcial
  @special = true
end

def special?
  @special
end

Now all you have to do to enable these checks:

s = SomeModel.new
s.make_special
+8
source

As you explained in the comments, you want to skip checking for new entries. In this case, you can use thomasfedb's answer, but don't use a variable @special, but:

validates_presence_of :something, :if => :persisted?

User s, User s. . API persisted?.

+4

This is a bit outdated. But I found http://apidock.com/rails/Object/with_options to be a good way to handle this behavior.

+1
source

All Articles