Rails Custom Validation error message
Without getting into all the javascript for a textfield, I'd like the following validation:
validates_length_of :brief_description,
:maximum=>250,
:message => "Brief is #{self.brief_description.length} long (max is 250)"
But the self.brief_description reference in the message doesn't work. What's the best way to do this?
I tried #{params[:brief_description].length} as well, but the m开发者_JAVA技巧odel doesn't know anything about params...
I could be wrong but I think you can try enclosing the string in single-quotes, and it might defer evaluating the string until runtime.
Short of that, you could write a custom validation (that doesn't use the validates_length_of macro) and do something like:
def validate_brief_description
errors.add :brief_description, "Brief is #{brief_description.length} long (max is 250)" if brief_description.length > 250
end
精彩评论