Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding model validation errors in rescue

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 am using <%= f.error_messages %> to display the error messages on the form.

I am using from_dt as a virtual attribute (string). The 'presence of' and 'format of' validation errors show up on the form, but when the user enters an invalid date format on the form and Date.Parse raises an exception I have a 'errors.add_to_base' statement in the rescue clause. Can anyone tell me why this error does not show up in the form error messages when I disable the 'format of' validation.

thanks.

like image 237
user290870 Avatar asked Dec 29 '22 21:12

user290870


1 Answers

Errors added outside of the validation callbacks will be removed when validation gets run - ActiveModel

So you have to add your errors from within a validation callback, not in a setter. This should work:

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"

  validate :from_dt_must_parse

  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
    @from_dt_parse_error = "not recognizable as a date"
  end

  def from_dt_must_parse
    self.errors[:from_dt] = @from_dt_parse_error
  end
end
like image 107
Brian Dunn Avatar answered Dec 31 '22 12:12

Brian Dunn