mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #24444 from prathamesh-sonpatki/update-message-proc-documentation
Update example of passing a proc to `:message` option for validating records [ci skip]
This commit is contained in:
commit
659b104a00
2 changed files with 10 additions and 4 deletions
|
@ -1,3 +1,9 @@
|
|||
* Update example of passing a proc to `:message` option for validating records.
|
||||
|
||||
This behavior was recently changed in https://github.com/rails/rails/pull/24119 to
|
||||
pass the object being validated as first argument to the `:message` proc
|
||||
instead of key of the field being validated.
|
||||
|
||||
## Rails 5.0.0.beta3 (February 24, 2016) ##
|
||||
|
||||
* No changes.
|
||||
|
|
|
@ -785,7 +785,7 @@ A `String` `:message` value can optionally contain any/all of `%{value}`,
|
|||
`%{attribute}`, and `%{model}` which will be dynamically replaced when
|
||||
validation fails.
|
||||
|
||||
A `Proc` `:message` value is given two arguments: a message key for i18n, and
|
||||
A `Proc` `:message` value is given two arguments: the object being validated, and
|
||||
a hash with `:model`, `:attribute`, and `:value` key-value pairs.
|
||||
|
||||
```ruby
|
||||
|
@ -801,10 +801,10 @@ class Person < ApplicationRecord
|
|||
# Proc
|
||||
validates :username,
|
||||
uniqueness: {
|
||||
# key = "activerecord.errors.models.person.attributes.username.taken"
|
||||
# object = person object being validated
|
||||
# data = { model: "Person", attribute: "Username", value: <username> }
|
||||
message: ->(key, data) do
|
||||
"#{data[:value]} taken! Try again #{Time.zone.tomorrow}"
|
||||
message: ->(object, data) do
|
||||
"Hey #{object.name}!, #{data[:value]} is taken already! Try again #{Time.zone.tomorrow}"
|
||||
end
|
||||
}
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue