aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorप्रथमेश Sonpatki <csonpatki@gmail.com>2016-03-24 19:58:13 +0530
committerप्रथमेश Sonpatki <csonpatki@gmail.com>2016-03-24 19:58:13 +0530
commita7d587f284b1360aec9c93695571520b6bdaac45 (patch)
treeb5bdc09dd65f0490ff80fca0bf51291c320e6c91 /guides
parent00a0388adcb2a09cb811ee9659636a154c36bca5 (diff)
parente6aaa8570ac4566bdd0cdfdf62ca1225ac22a67c (diff)
downloadrails-a7d587f284b1360aec9c93695571520b6bdaac45.tar.gz
rails-a7d587f284b1360aec9c93695571520b6bdaac45.tar.bz2
rails-a7d587f284b1360aec9c93695571520b6bdaac45.zip
Merge pull request #24254 from vijayagrawal18/custom-context-documentation
Added missing custom context validation documentation
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_record_validations.md19
1 files changed, 19 insertions, 0 deletions
diff --git a/guides/source/active_record_validations.md b/guides/source/active_record_validations.md
index 10bd201145..bcfdb935b2 100644
--- a/guides/source/active_record_validations.md
+++ b/guides/source/active_record_validations.md
@@ -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
------------------