Rails: Order of Validations Determines Order of Error Messages

A small but interesting note:

The order of validation statements in the model determines the order the associated error messages will appear on a form’s web page.

IMHO, errors should appear in the same order as the fields on the form being validated. So, if you have a form that looks like:

Then the validation statements should be in a corresponding order in the model:

  validates :name, :presence => true
  validates :distance, :numericality => true

If both fields fail validation the resulting list of errors will appear in the same order as the fields are placed on the form:

Posted in tech | Tagged | Leave a comment