Austin
Austin

Reputation: 3890

preventing rails validation based on previous validation

I have a model with 2 validations on the 'name' attribute. It goes something like this:

validates :name, :uniqueness => true

validate do
  errors.add(:name, "is dumb") if name_is_dumb?
end

I don't want the 2nd validation to run if the first validation fails (the name is not unique).

What's the best and cleanest way to do this?

Upvotes: 1

Views: 160

Answers (1)

khelll
khelll

Reputation: 24020

According to the documentation:

Callbacks are generally run in the order they are defined, with the exception of callbacks defined as methods on the model, which are called last.

So the following snippet should work:

validates :name, :uniqueness => true

validate do
  errors.add(:name, "is dumb") unless errors[:name].nil?
end

Upvotes: 2

Related Questions