ruby - Display invalid value for Date in Rails -
i have custom date validator looks this:
# app/validators/date_validator.rb class datevalidator < activemodel::eachvalidator date_regex = /\d{2}\/\d{2}\/\d{4}/ def validate_each(record, attribute, value) before_type_cast = "#{attribute}_before_type_cast" raw_value = record.send(before_type_cast) if record.respond_to?(before_type_cast.to_sym) raw_value ||= value return if raw_value.blank? record.errors[attribute] << (options[:message] || "must in correct date format") if (raw_value =~ date_regex).nil? end end in model, use validator following code:
validates :date_assigned :date => true my problem is, when user enters invalid date, i'd display invalid date on form can correct it. currently, when enter "11111" date assigned field on form, believe rails typecasting converting nil (date_assigned date value in db), instead of "11111" being displayed, nothing is.
if user enters "01/01/01" (invalid per date regex), view displaying "0001-01-01" instead of original (invalid) string of "01/01/01".
can me figure out how this? thanks
Comments
Post a Comment