-
-
Save PerezIgnacio/030654294e5a129e4ab51a9bcf22669c to your computer and use it in GitHub Desktop.
Custom validator example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyValidator < ActiveModel::Validator | |
def validate(record) | |
return if record.name.starts_with? 'X' | |
record.errors[:name] << 'Need a name starting with X please!' | |
end | |
end | |
class Person < ApplicationRecord | |
validates_with MyValidator | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment