-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add error message value formatter to the numericality validator #41482
Conversation
Makes sense. Can you add a CHANGELOG entry? |
@rafaelfranca done |
@joaomangilli Do you have a use-case in mind that would benefit from validates_numericality_of :approved, greater_than: 50000,
message: -> (record, data) { "must be greater than #{do_something(data[:count])}" } Perhaps this could be improved by allowing the validates_numericality_of :approved, greater_than: 50000,
message: -> (record, data) { data.merge(count: do_something(data[:count])) } |
@jonathanhefner thanks! I think this is a best way to solve the problem. This can pave the way for solving more problems. I'll make the change. |
@joaomangilli To clarify: I'm not sure if returning a Hash from a Another possibility might be using validates_numericality_of :approved, greater_than: 50000, message: -> (record, data) do
data[:count] = do_something(data[:count])
record.errors.generate_message(data[:attribute_symbol], data[:type], data)
end |
@jonathanhefner sorry, I wanted to say that it looks like a better (not the best) solution. But the more I think about it the more I think that this validator should offer a native option to format the count value, without the need to deal with the message. I know that this value is part of the message, but I think it makes sense to pass this value already formatted, so we don't have to deal with it in the message. Because to do this in the message, we need to change the method of the message to offer more options or flexibility and it still won't solve the problem, as we will still need to use this method to "interpolate" the formatted value in the message. What do you think? |
@rafaelfranca can we go ahead with this? |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Summary
Sometimes, we need to format the value being shown in an error message in the numericality validator (like
greater_than_or_equal_to: 25
, we want to format the number 25 to something like 25.00) to match a monetary value or something like that.This PR opens an option to format that value the way you need it.