1

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.

Ricardo Francois
  • 752
  • 7
  • 24
  • 1
    Get the message from where? Is this a model-level validation? We need code for context. – tadman Apr 01 '21 at 06:13
  • I'm not entirely sure where this message is coming from. I'd presume it's built-in within `date_select` for invalid date combinations as I haven't written this code. If I were to guess, it must be a model-level validation and the flash notice message appears on my view once I try to submit my form with an invalid date. – Ricardo Francois Apr 01 '21 at 06:22
  • 1
    Provide your form code and model – honey Apr 01 '21 at 06:28
  • Not sure how it's relevant but added – Ricardo Francois Apr 01 '21 at 06:42
  • Where and how is the error message showing up? Do it look like a default Rails error message or as a message from the browser? Do you use a frontend library (javascript) that might create that message? – spickermann Apr 01 '21 at 06:51
  • Looks like one of the default green Rails flash messages that prevents me from submitting the form. Not using any JS or front-end library. Might be relevant to this https://stackoverflow.com/questions/30508093/change-error-message-invalid-date-in-input-type-date but I don't have any JS – Ricardo Francois Apr 01 '21 at 07:00
  • What does your HTML look like ? An `` has built-in browser validation, I'm not sure that Rails has anything to do with that. – Fravadona Apr 01 '21 at 07:52
  • I see `
    invalid date
    ` when I Inspect Element my webpage but the div tag is not anywhere in the code that I've written
    – Ricardo Francois Apr 01 '21 at 08:10

1 Answers1

1

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.

Lam Phan
  • 3,405
  • 2
  • 9
  • 20
  • Really thought this would work but unfortunately I'm still getting the original "invalid date" message. I'm using Rails 6.0.3.6. – Ricardo Francois Apr 01 '21 at 22:18
  • 1
    In that case, I thought that in your code (maybe you override ActiveRecord save/update, are you ?) some where throw directly the ArgumentError, 'invalid date' without add to `ActiveModel.errors`. (for example: you can test by add one line code `"31/04/2021".to_date`). In that case, you need to wrap your code in begin .. rescue => error, then format that error then add to your `ActiveModel.errors`. – Lam Phan Apr 02 '21 at 02:14
  • Yup, that's exactly what I ended up doing actually! Your comment definitely put me on the right path – Ricardo Francois Apr 04 '21 at 05:03