date_time_attribute icon indicating copy to clipboard operation
date_time_attribute copied to clipboard

Validations

Open wvengen opened this issue 10 years ago • 3 comments

Using date_time_attribute with a form with a date and time text field, I find that entering an invalid value (e.g. "foo" as the time) either throws an exception (default parser) or is silently ignored (Chronic parser).

Looking at the container source code, I see that the fields are directly passed through the parser, so the original value is not even present in the model. How could validations be done?

wvengen avatar May 30 '14 19:05 wvengen

@wvengen that's a good point. Will figure out tomorrow how to fix that. As a quick workaround you can quickly fix that by using virtual attribute in your model.

class MyModel < Ar::Base
   attr_accessor :starts_date_value, :starts_time_value

   validate_presense_of :starts_date_value

   def starts_date_value=(val)
     @starts_date_value = val
     self.starts_date = val # if using Chronic
   end
end

einzige avatar May 31 '14 16:05 einzige

Thanks! Looking forward to see if this could become part of the gem.

wvengen avatar Jun 01 '14 15:06 wvengen

I've found a workaround for now - by including DateTimeAttributeValidate which extends DateTimeAttribute.

wvengen avatar Jun 02 '14 13:06 wvengen