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:

This entry was posted in tech and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>