I'm using the date_select
method for my form in Rails.
Whenever I choose an invalid date such as February 30th or April 31st, I get a default notice message stating, "invalid date" which I believe is built-in.
How would I change this to something custom like "The date you have entered is invalid." ?
I'd like for this message to be consistent with the capitalization and punctuation in the rest of my app.
Rails raise an ArgumentError with hard core message 'invalid date' whenever convert time failed (extension Time-Zone and Time-Calculations)
But in your case, we don't need to intercept or override any code in models/controllers, just use a helper method to convert 'invalid date' message (or whatever messages you want) before they're rendered, right ?
# helper
def format_error(error)
return ArgumentError.new(I18n.t 'date.invalid') if error.class == ArgumentError && error.message == 'invalid date'
error
end
# view
<% interval.errors.full_messages.each do |message| %>
<li><%= format_error(message) %></li>
<% end %>
By the way, what version of Rails are you using ? I use Rails 6 and whenever i pick wrong day it will be automatically converted to next valid day, for example: i pick 31/4 -> then it'll be saved as 1/5.