mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
outline two ways to invoke a custom active model validator
This commit is contained in:
@@ -162,13 +162,21 @@ modules:
|
||||
{Learn more}[link:classes/ActiveModel/Validations.html]
|
||||
|
||||
* Custom validators
|
||||
|
||||
|
||||
class Person
|
||||
include ActiveModel::Validations
|
||||
validates_with HasNameValidator
|
||||
attr_accessor :name
|
||||
end
|
||||
|
||||
class Person
|
||||
include ActiveModel::Validations
|
||||
validates :name, :has_name => true
|
||||
attr_accessor :name
|
||||
end
|
||||
|
||||
You could use any of the two ways mentioned above to use a Custom Validator.
|
||||
|
||||
class HasNameValidator < ActiveModel::Validator
|
||||
def validate(record)
|
||||
record.errors[:name] = "must exist" if record.name.blank?
|
||||
|
||||
Reference in New Issue
Block a user