mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #20795 from benjiwheeler/validation_guide_ret_val
activerecord guide: clarified roles of valid method and errors object [ci skip]
This commit is contained in:
commit
cceaa5c853
1 changed files with 15 additions and 7 deletions
|
@ -149,8 +149,10 @@ false` as an argument. This technique should be used with caution.
|
||||||
|
|
||||||
### `valid?` and `invalid?`
|
### `valid?` and `invalid?`
|
||||||
|
|
||||||
To verify whether or not an object is valid, Rails uses the `valid?` method.
|
Before saving an ActiveRecord object, Rails runs your validations.
|
||||||
You can also use this method on your own. `valid?` triggers your validations
|
If these validations produce any errors, Rails does not save the object.
|
||||||
|
|
||||||
|
You can also run these validations on your own. `valid?` triggers your validations
|
||||||
and returns true if no errors were found in the object, and false otherwise.
|
and returns true if no errors were found in the object, and false otherwise.
|
||||||
As you saw above:
|
As you saw above:
|
||||||
|
|
||||||
|
@ -168,8 +170,9 @@ through the `errors.messages` instance method, which returns a collection of err
|
||||||
By definition, an object is valid if this collection is empty after running
|
By definition, an object is valid if this collection is empty after running
|
||||||
validations.
|
validations.
|
||||||
|
|
||||||
Note that an object instantiated with `new` will not report errors even if it's
|
Note that an object instantiated with `new` will not report errors
|
||||||
technically invalid, because validations are not run when using `new`.
|
even if it's technically invalid, because validations are automatically run
|
||||||
|
only when the object is saved, such as with the `create` or `save` methods.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Person < ActiveRecord::Base
|
class Person < ActiveRecord::Base
|
||||||
|
@ -986,6 +989,10 @@ class method, passing in the symbols for the validation methods' names.
|
||||||
You can pass more than one symbol for each class method and the respective
|
You can pass more than one symbol for each class method and the respective
|
||||||
validations will be run in the same order as they were registered.
|
validations will be run in the same order as they were registered.
|
||||||
|
|
||||||
|
The `valid?` method will verify that the errors collection is empty,
|
||||||
|
so your custom validation methods should add errors to it when you
|
||||||
|
wish validation to fail:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Invoice < ActiveRecord::Base
|
class Invoice < ActiveRecord::Base
|
||||||
validate :expiration_date_cannot_be_in_the_past,
|
validate :expiration_date_cannot_be_in_the_past,
|
||||||
|
@ -1005,9 +1012,10 @@ class Invoice < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
By default such validations will run every time you call `valid?`. It is also
|
By default, such validations will run every time you call `valid?`
|
||||||
possible to control when to run these custom validations by giving an `:on`
|
or save the object. But it is also possible to control when to run these
|
||||||
option to the `validate` method, with either: `:create` or `:update`.
|
custom validations by giving an `:on` option to the `validate` method,
|
||||||
|
with either: `:create` or `:update`.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Invoice < ActiveRecord::Base
|
class Invoice < ActiveRecord::Base
|
||||||
|
|
Loading…
Reference in a new issue