I have the following model with a virtual attribute
class Mytimeperiod < ActiveRecord::Base validates presence of :from_dt validates_format_of :from_dt, :with => /\A\d{2}\/\d{2}\/\d{4}\Z/, :message => "format is mm/dd/yyyy" def from_dt self.from_date.strftime("%m/%d/%Y") if !self.from_date.blank? end def from_dt=(from_dt) self.from_date = Date.parse(from_dt) rescue self.errors.add_to_base("invalid from dt") end end
I use <%= f.error_messages %> to display error messages on the form.
I use from_dt as a virtual attribute (string). The "presence form" and "format" of validation errors are displayed on the form, but when the user enters an invalid date format on the form, and Date.Parse throws an exception, I have an "errors.add_to_base" statement in the rescue clause, can anyone tell to me why this error does not appear in form error messages when I turn off the "format" check.
thanks.
source share