I’m writing my first custom rails validation, and would like to tag the offending class with an html “error” class if they return false – I can’t quite figure out how to do it. Relevant validation code below – any help appreciated.
(If it makes a difference, I’m using jQuery)
validates_each :shop do |record, attr, value|
shopvar = record.shops.map{ |s| s.email.downcase.strip }
if shopvar.count != shopvar.uniq.count
record.errors.add(attr, 'has the same email address entered more than once')
#record.errors[attr] << "You have entered this shop in the form twice"
end
end
So in your form you’d have something like this for an input field
Since errors is a hash you could use the “include?” method like so…
This tells you that there’s something wrong with this field. Now all you need to do is style it.
Whack on a ternary operator asi…
Done.