{"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 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 The question is: if the user just left the field blank, why we should show all those errors to him? Are they all relevant?<\/p>\n OMG! What have I done wrong to appear so many errors?<\/p><\/div>\n Luckily, the solution is quite simple: we can make use of the :allow_blank<\/em> option, so other validations won’t be triggered if the field is blank:<\/p>\n This is also a nice use case for the Object#with_options<\/strong> method added by Rails:<\/p>\n SSN is just an example, but we are frequently burdening our users in username, e-mail, password and many other fields.<\/p>\n Another interesting subject about validations are the confirmation fields. We have the following note in the documentation of validates_confirmation_of<\/em>:<\/p>\n And this is a feature<\/strong>. This means that you don’t need to give the :password_confirmation<\/em> key when creating objects in console or when writing tests:<\/p>\n If the test fails, you are validating the presence of :password_confirmation<\/em>, which is unnecessary. Since the :password_confirmation<\/em> field will be available in the views, it will always be sent. So if the user leave it blank, it’s sent as blank value to the model and therefore will be checked. Just in the place it needs to be.<\/p>\n","protected":false},"excerpt":{"rendered":" One of the first things we learn in Rails which are greatly useful are ActiveRecord validations. 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. For instance, let’s suppose we are validating the Social Security Number (SSN) of an … \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[7,21,20],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/123"}],"collection":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":8,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":1982,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/123\/revisions\/1982"}],"wp:attachment":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\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
\n
\r\nclass User < ActiveRecord::Base\r\n validates_presence_of :ssn\r\n validates_length_of :ssn, :is => 11, :allow_blank => true\r\n validates_numericality_of :ssn, :allow_blank => true\r\n validates_uniqueness_of :ssn, :allow_blank => true\r\n validates_as_ssn :ssn, :allow_blank => true\r\nend\r\n<\/pre>\n
\r\nclass User < ActiveRecord::Base\r\n validates_presence_of :ssn\r\n\r\n with_options :allow_blank => true do |v|\r\n v.validates_length_of :ssn, :is => 11\r\n v.validates_numericality_of :ssn\r\n v.validates_uniqueness_of :ssn\r\n v.validates_as_ssn :ssn\r\n end\r\nend\r\n<\/pre>\n
Do not validate presence of confirmation fields<\/h3>\n
\r\nvalidates_confirmation_of :password\r\n# NOTE: This check is performed only if password_confirmation is not nil\r\n<\/pre>\n
\r\nit \"should be valid with valid attributes\" do\r\n User.new(:password => \"123456\").should be_valid\r\nend\r\n<\/pre>\n