1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

improve active record validation docs

This commit is contained in:
Manuel Meurer 2020-07-12 11:40:04 +02:00
parent badba9d2fa
commit 02a93f6afd
No known key found for this signature in database
GPG key ID: A61676F52806A868

View file

@ -558,8 +558,8 @@ Since `false.blank?` is true, if you want to validate the presence of a boolean
field you should use one of the following validations:
```ruby
validates :boolean_field_name, inclusion: { in: [true, false] }
validates :boolean_field_name, exclusion: { in: [nil] }
validates :boolean_field_name, inclusion: [true, false]
validates :boolean_field_name, exclusion: [nil]
```
By using one of these validations, you will ensure the value will NOT be `nil`
@ -684,7 +684,7 @@ validator class as `options`:
```ruby
class GoodnessValidator < ActiveModel::Validator
def validate(record)
if options[:fields].any?{|field| record.send(field) == "Evil" }
if options[:fields].any? { |field| record.send(field) == "Evil" }
record.errors.add :base, "This person is evil"
end
end
@ -799,9 +799,9 @@ class Person < ApplicationRecord
# Hard-coded message
validates :name, presence: { message: "must be given please" }
# Message with dynamic attribute value. %{value} will be replaced with
# the actual value of the attribute. %{attribute} and %{model} also
# available.
# Message with dynamic attribute value. %{value} will be replaced
# with the actual value of the attribute. %{attribute} and %{model}
# are also available.
validates :age, numericality: { message: "%{value} seems wrong" }
# Proc
@ -810,7 +810,7 @@ class Person < ApplicationRecord
# object = person object being validated
# data = { model: "Person", attribute: "Username", value: <username> }
message: ->(object, data) do
"Hey #{object.name}!, #{data[:value]} is taken already! Try again #{Time.zone.tomorrow}"
"Hey #{object.name}, #{data[:value]} is already taken."
end
}
end
@ -1260,9 +1260,9 @@ Assuming we have a model that's been saved in an instance variable named
<h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% @article.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
<% @article.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
@ -1274,7 +1274,7 @@ the entry.
```html
<div class="field_with_errors">
<input id="article_title" name="article[title]" size="30" type="text" value="">
<input id="article_title" name="article[title]" size="30" type="text" value="">
</div>
```