mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Documentation cleanup and linkage for validator
This commit is contained in:
parent
907754d7ee
commit
3cb0283fb6
2 changed files with 26 additions and 2 deletions
|
@ -193,3 +193,26 @@ functionality from the following modules:
|
|||
|
||||
{Learn more}[link:classes/ActiveModel/Validations.html]
|
||||
|
||||
* Make custom validators
|
||||
|
||||
class Person
|
||||
include ActiveModel::Validations
|
||||
validates_with HasNameValidator
|
||||
attr_accessor :name
|
||||
end
|
||||
|
||||
class HasNameValidator < ActiveModel::Validator
|
||||
def validate(record)
|
||||
record.errors[:name] = "must exist" if record.name.blank?
|
||||
end
|
||||
end
|
||||
|
||||
p = ValidatorPerson.new
|
||||
p.valid? #=> false
|
||||
p.errors.full_messages #=> ["Name must exist"]
|
||||
p.name = "Bob"
|
||||
p.valid? #=> true
|
||||
|
||||
{Learn more}[link:classes/ActiveModel/Validator.html]
|
||||
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
module ActiveModel #:nodoc:
|
||||
# A simple base class that can be used along with ActiveModel::Validations::ClassMethods.validates_with
|
||||
# A simple base class that can be used along with
|
||||
# +ActiveModel::Validations::ClassMethods.validates_with+
|
||||
#
|
||||
# class Person
|
||||
# include ActiveModel::Validations
|
||||
|
@ -28,7 +29,7 @@ module ActiveModel #:nodoc:
|
|||
# end
|
||||
#
|
||||
# class MyValidator < ActiveModel::Validator
|
||||
# def validate
|
||||
# def validate(record)
|
||||
# record # => The person instance being validated
|
||||
# options # => Any non-standard options passed to validates_with
|
||||
# end
|
||||
|
|
Loading…
Reference in a new issue