date_time_attribute
date_time_attribute copied to clipboard
Validations
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 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
Thanks! Looking forward to see if this could become part of the gem.
I've found a workaround for now - by including DateTimeAttributeValidate
which extends DateTimeAttribute
.