Validation Helpers

Here are some of the common validation helpers in Ruby on Rails

Here is a list of some of the common validation helpers in Ruby on Rails;

  • validates_acceptance_of
  • validates_associated
  • validates_confirmation_of
  • validates_each
  • validates_exclusion_of
  • validates_format_of
  • validates_inclusion_of
  • validates_length_of
  • validates_numericality_of
  • validates_presence_of
  • validates_uniqueness_of

validates_acceptance_of is used to see if someone has checked a checkbox during a form submission, the attribute does not need to have an associated database column.

validates_associated is used to see if the objects you are associating with your model are valid objects.

validates_confirmation_of is used when confirming things like passwords the convention is that you would confirm the attribute password against the attribute password_confirm.

validates_each is used to validate attributes against a block of code.

validates_exclusion_of is used to validate if an attribute is not in a particular enumerable object.

validates_format_of is used to validate the format of an attribute. This is handled with a regular expression and can be used to make sure a pattern does not exist within the attribute.

validates_inclusion_of is used as the opposite of exclusion, it makes sure the attribute is found within an enumerable object.

validates_length_of is used to verify that the length of the attribute matches the restrictions that have been put in place around it.

validates_numericality_of is used to ensure something is a number and can go as far to say a number greater than or less than something, or force it to only be an integer.

validates_presence_of is used to ensure that the attribute exists and is not nil when it is being saved.

validates_uniqueness_of is used to ensure that there are no other attributes with the same information in the system.

I hope this helps someone as an uber quick reference.

 

Recent posts View all

Freelancing

Getting the most out of your agency

Here are some tips based on years of working with, for, and as an agency on how to get the most out of any work you do.

VS Code Web Dev

Select multiple lines in VS Code

How to select multiple lines to edit at once within VS Code