I have custom validation in my model very similar to this one:
and the solution offered just works:
errors.add(:base, 'Start date must be before stop date') if self.start > self.stop
Using :base causes just 1 error, and it’s fine because I don’t want two separate errors showing the same here. However, no fields are highlighted, and I need the :start and :stop fields highlighted too, as if I were using :start or :stop in errors.add.
How can I do that?
You should leverage Rails built in mechanisms: they consist in wrapping the form fields containing errors with a
divwithfield_with_errorclass.If this doesn’t suit you:
customize it, see Railscast
more manual solution, check which errors your instance bears and act accordingly, ie
@instance.errors.include? :filed_name. In this scope, don’t add all your errors tobase