{"id":123,"date":"2009-08-31T11:02:15","date_gmt":"2009-08-31T14:02:15","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=123"},"modified":"2011-04-08T09:57:02","modified_gmt":"2011-04-08T12:57:02","slug":"do-not-burden-your-users-with-validations","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2009\/08\/do-not-burden-your-users-with-validations\/","title":{"rendered":"Do not burden your users with validations"},"content":{"rendered":"

One of the first things we learn in Rails which are greatly useful are ActiveRecord validations<\/a>. However, since they are easy to add, it happens frequently that we are burdening our users with validations instead of making forms easier and clearer.<\/p>\n

For instance, let’s suppose we are validating the Social Security Number (SSN)<\/a> of an user on signup. A sample code would be:<\/p>\n

\r\nclass User < ActiveRecord::Base\r\n  validates_presence_of :ssn\r\n  validates_length_of :ssn, :is => 9\r\n  validates_numericality_of :ssn\r\n  validates_uniqueness_of :ssn\r\n  validates_as_ssn :ssn # Checks if a reserved or special SSN was sent\r\nend\r\n<\/pre>\n

With the configuration above, if the user forgets to fill in the SSN, leaving it blank, four error messages<\/strong> related to the SSN field will be shown:<\/p>\n