Validation Methods
Ruby on Rails Validation methods
- validates_presence_of
Attribute mustn’t be blank(nil,false,[],{})
1
|
|
- validates_length_of
Attribute must meet length requirements :is, :minimum, :maximum, :within, :in, :too_short, :too_long
1
|
|
- validates_numericality_of
Attribute must be integer or floating number :less_than, :greater_than, :equal_to, :odd, :even, :greater_than_or_equal_to
1
|
|
- validates_inclusion_of
Attribute must be in a list of choises. (array or range) :in
- validates_exclusion_of
Attribute mustn’t be in a list of choises. (array or range) :in
- validates_format_of
Attribute must match a regular expression. :with
- validates_uniqueness_of
Attribute mustn’t exist in the database :case_sensitive, :scope
- validates_acceptance_of
Attribute must be accepted :accept(expected value, 1)
- validates_confirmation_of
Attribute must be confirmed by entering it twice creates a virtual blank attribute for confirmation.
- validates_associated
Associated object or objects must all be valid.
1 2 3 4 |
|
Example of Validations
1 2 3 |
|
When we use like above, We diverge to DRY, instead of above
1 2 3 |
|
That’s all.