validates :attribute, validator_name: options
You can memorize the core validators in 10 seconds:
| Validator | Purpose | Example |
|---|---|---|
presence: |
must not be blank | presence: true |
absence: |
must be blank | absence: true |
confirmation: |
requires a matching field | confirmation: true |
acceptance: |
typical checkbox | terms: acceptance: true |
numericality: |
must be number | numericality: true |
length: |
length constraints | length: { max: 100 } |
inclusion: |
value must be in list/range | inclusion: 1..5 |
exclusion: |
must not be in list | exclusion: ["admin"] |
format: |
regex match | format: /@/ |
Rails maps the
validator_name to a validator class:
presence: → PresenceValidator
length: → LengthValidator
numericality: → NumericalityValidator
format: → FormatValidator
custom ones → EmailValidator