Rails, custom validation and error not showing up
I've currently five boolean attributes. I do a custom validation on them :
def limit_impacts
i = 0
choices = [self.first_attr, self.sec_attr, self.third_attr,
self.fourth_attr, self.fifth_attr]
choices.each do |choice|
if choice == true
i+=1
end
end
if i > 2
errors[:base] << ("There must be one or two impacts.")
end
end
The idea is to test if more than two of them are set to true, if it's the
case, set an error. I'm setting a :base error because it's not related
directly to only one attribute.
I'm simply doing this for my validation : validate :limit_impacts
and the part of the view handling this :
= f.input :first_attr, :as => :boolean
= f.input :sec_attr, :as => :boolean
= f.input :third_attr, :as => :boolean
= f.input :fouth_attr, :as => :boolean
= f.input :fifth_attr, :as => :boolean
The problem is, when I check more than 2 checkboxes the entrie is not
saving and that's normal, but no error message is showing up in the view.
What am I doing wrong ?
No comments:
Post a Comment