5.8. Adding Validations

Validations are a mechanism provided by Active Record to ensure that the data conforms to certain business rules. The forms generated by the scaffold generator don't perform any validations by default. For example, you could create an article that had an empty title and empty body, and these would be stored in the database as empty strings. Similarly, you may decide that you don't want to allow articles to have the same exact text, therefore eliminating duplicates. Validations are the answer.

Change the Article model to look like this (appmodelsarticle.rb):

class Article < ActiveRecord::Base
  validates_presence_of :title, :body
  validates_uniqueness_of :body
end

Those two highlighted lines indicate that an article's title and a body are required and that the body of an article needs to be different from any others that already exist. Save the file, head over to /articles/new, and try to click Create Article with the title and body left empty. You should see an error report similar to the one shown in Figure 5-12.

NOTE

Unlike ASP.NET validation controls, Rails validations are always server-side and defined in the model, as opposed to the page that gets rendered.

Figure 5.12. Figure 5-12

That was quite effortless! If you take a look at the source of the generated page, you will see that the <%= f.error_messages %> inside the partial was transformed into (reformatted for clarity):

<div class="errorExplanation" id="errorExplanation">
<h2>2 errors prohibited this article from being saved</h2>
<p>There were problems with the following fields:</p>
<ul><li>Body can't be blank</li><li>Title can't be blank</li></ul>
</div>

You'll notice also that the affected fields are now surrounded by a <div> tag, which highlights them:

<p>
<div class="fieldWithErrors"><label for="article_title">Title</label></div>
<br />
<div class="fieldWithErrors"><input id="article_title" name="article[title]"
size="30" type="text" value="" /></div>
</p>

<p>
<div class="fieldWithErrors"><label for="article_body">Body</label></div>

<br />
<div class="fieldWithErrors"><textarea cols="40" id="article_body"
name="article[body]" rows="20"></textarea></div>
</p>

The CSS classes errorExplanation and fieldWithErrors are defined in the scaffold.css file in publicstylesheets, so you can customize the look and feel of error reporting to suit your own tastes. You can also define your own custom messages by passing the :message option to the validation. For example, you can specify that the report should print "Body is a required field" and "Title is a required field" instead of the default "can't be blank," as follows:

validates_presence_of :title, :body, :message => "is a required field"

Notice that even though the two attributes that you are trying to validate are passed to the method on the same line, their validation is independent from each other. This means that if you tried to submit an article with a non-empty body but an empty title, you'd get an error message about the missing title only. Similarly, if you tried to create a duplicate post with the exact same text as a previous one, a message would inform you that this is not allowed.

Chapter 7 explains many more useful validations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.68.28