Adding model validation errors during 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 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.

+4
source share
3 answers

Errors added outside validation callbacks will be deleted when validation starts - ActiveModel

Thus, you should add your errors from the validation callback, and not to the 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 
+11
source

Your syntax looks fine. If you have problems with testing on the console, be sure to reload! (or run and run script/console again).

If the only reason you use the virtual attribute is to check the date format, remember that you can do your own validation using the validate (untested) method:

 class Mytimeperiod < ActiveRecord::Base protected def validate date = Date.parse(from_date) rescue errors.add("from_date", "date is invalid") end end 

This has the advantage of adding errors to the field rather than to the object.

0
source

I believe the problem is just your syntax, you need to start adding begin to the beginning of the block that has rescue :

 def from_dt=(from_dt) begin self.from_date = Date.parse(from_dt) rescue self.errors.add_to_base("invalid from dt") end end 
-1
source

Source: https://habr.com/ru/post/1306170/


All Articles