mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
[ci skip] Added missing custom context validation
This commit is contained in:
parent
00a0388adc
commit
e6aaa8570a
1 changed files with 19 additions and 0 deletions
|
@ -830,6 +830,25 @@ class Person < ApplicationRecord
|
|||
end
|
||||
```
|
||||
|
||||
You can also use `on:` to define custom context.
|
||||
Custom contexts need to be triggered explicitly
|
||||
by passing name of the context to `valid?`, `invalid?` or `save`.
|
||||
|
||||
```ruby
|
||||
class Person < ApplicationRecord
|
||||
validates :email, uniqueness: true, on: :account_setup
|
||||
validates :age, numericality: true, on: :account_setup
|
||||
end
|
||||
|
||||
person = Person.new
|
||||
```
|
||||
|
||||
`person.valid?(:account_setup)` executes both the validations
|
||||
without saving the model. And `person.save(context: :account_setup)`
|
||||
validates `person` in `account_setup` context before saving.
|
||||
On explicit triggers, model is validated by
|
||||
validations of only that context and validations without context.
|
||||
|
||||
Strict Validations
|
||||
------------------
|
||||
|
||||
|
|
Loading…
Reference in a new issue