mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Moved validates_each to the last position of it's section. Added VIM swap files to .gitignore
This commit is contained in:
parent
2c2824a967
commit
afdacd0857
3 changed files with 39 additions and 38 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -15,3 +15,4 @@ railties/test/500.html
|
|||
railties/doc/guides/html/images
|
||||
railties/doc/guides/html/stylesheets
|
||||
*.rbc
|
||||
*.swp
|
||||
|
|
|
@ -221,8 +221,6 @@ ul#navMain {
|
|||
|
||||
<li><a href="#_the_tt_validates_confirmation_of_tt_helper">The <tt>validates_confirmation_of</tt> helper</a></li>
|
||||
|
||||
<li><a href="#_the_tt_validates_each_tt_helper">The <tt>validates_each</tt> helper</a></li>
|
||||
|
||||
<li><a href="#_the_tt_validates_exclusion_of_tt_helper">The <tt>validates_exclusion_of</tt> helper</a></li>
|
||||
|
||||
<li><a href="#_the_tt_validates_format_of_tt_helper">The <tt>validates_format_of</tt> helper</a></li>
|
||||
|
@ -237,6 +235,8 @@ ul#navMain {
|
|||
|
||||
<li><a href="#_the_tt_validates_uniqueness_of_tt_helper">The <tt>validates_uniqueness_of</tt> helper</a></li>
|
||||
|
||||
<li><a href="#_the_tt_validates_each_tt_helper">The <tt>validates_each</tt> helper</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
<li>
|
||||
|
@ -504,20 +504,7 @@ http://www.gnu.org/software/src-highlite -->
|
|||
validates_presence_of <span style="color: #990000">:</span>email_confirmation
|
||||
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
|
||||
<div class="paragraph"><p>The default error message for <tt>validates_confirmation_of</tt> is "<em>doesn’t match confirmation</em>"</p></div>
|
||||
<h3 id="_the_tt_validates_each_tt_helper">3.4. The <tt>validates_each</tt> helper</h3>
|
||||
<div class="paragraph"><p>This helper validates attributes against a block. It doesn’t have a predefined validation function. You should create one using a block, and every attribute passed to <tt>validates_each</tt> will be tested against it. In the following example, we don’t want names and surnames to begin with lower case.</p></div>
|
||||
<div class="listingblock">
|
||||
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
||||
by Lorenzo Bettini
|
||||
http://www.lorenzobettini.it
|
||||
http://www.gnu.org/software/src-highlite -->
|
||||
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
||||
validates_each <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>surname <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>model<span style="color: #990000">,</span> attr<span style="color: #990000">,</span> value<span style="color: #990000">|</span>
|
||||
model<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>add<span style="color: #990000">(</span>attr<span style="color: #990000">,</span> <span style="color: #FF0000">'Must start with upper case'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> value <span style="color: #990000">=~</span> <span style="color: #FF6600">/^[a-z]/</span>
|
||||
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
||||
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
|
||||
<div class="paragraph"><p>The block receives the model, the attribute’s name and the attribute’s value. If your validation fails, you can add an error message to the model, therefore making it invalid.</p></div>
|
||||
<h3 id="_the_tt_validates_exclusion_of_tt_helper">3.5. The <tt>validates_exclusion_of</tt> helper</h3>
|
||||
<h3 id="_the_tt_validates_exclusion_of_tt_helper">3.4. The <tt>validates_exclusion_of</tt> helper</h3>
|
||||
<div class="paragraph"><p>This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.</p></div>
|
||||
<div class="listingblock">
|
||||
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
||||
|
@ -530,7 +517,7 @@ http://www.gnu.org/software/src-highlite -->
|
|||
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
|
||||
<div class="paragraph"><p>The <tt>validates_exclusion_of</tt> helper has an option <tt>:in</tt> that receives the set of values that will not be accepted for the validated attributes. The <tt>:in</tt> option has an alias called <tt>:within</tt> that you can use for the same purpose, if you’d like to. In the previous example we used the <tt>:message</tt> option to show how we can personalize it with the current attribute’s value, through the <tt>%s</tt> format mask.</p></div>
|
||||
<div class="paragraph"><p>The default error message for <tt>validates_exclusion_of</tt> is "<em>is not included in the list</em>".</p></div>
|
||||
<h3 id="_the_tt_validates_format_of_tt_helper">3.6. The <tt>validates_format_of</tt> helper</h3>
|
||||
<h3 id="_the_tt_validates_format_of_tt_helper">3.5. The <tt>validates_format_of</tt> helper</h3>
|
||||
<div class="paragraph"><p>This helper validates the attributes’s values by testing if they match a given pattern. This pattern must be specified using a Ruby regular expression, which must be passed through the <tt>:with</tt> option.</p></div>
|
||||
<div class="listingblock">
|
||||
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
||||
|
@ -542,7 +529,7 @@ http://www.gnu.org/software/src-highlite -->
|
|||
<span style="color: #990000">:</span>message <span style="color: #990000">=></span> <span style="color: #FF0000">"Only letters allowed"</span>
|
||||
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
|
||||
<div class="paragraph"><p>The default error message for <tt>validates_format_of</tt> is "<em>is invalid</em>".</p></div>
|
||||
<h3 id="_the_tt_validates_inclusion_of_tt_helper">3.7. The <tt>validates_inclusion_of</tt> helper</h3>
|
||||
<h3 id="_the_tt_validates_inclusion_of_tt_helper">3.6. The <tt>validates_inclusion_of</tt> helper</h3>
|
||||
<div class="paragraph"><p>This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.</p></div>
|
||||
<div class="listingblock">
|
||||
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
||||
|
@ -555,7 +542,7 @@ http://www.gnu.org/software/src-highlite -->
|
|||
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
|
||||
<div class="paragraph"><p>The <tt>validates_inclusion_of</tt> helper has an option <tt>:in</tt> that receives the set of values that will be accepted. The <tt>:in</tt> option has an alias called <tt>:within</tt> that you can use for the same purpose, if you’d like to. In the previous example we used the <tt>:message</tt> option to show how we can personalize it with the current attribute’s value, through the <tt>%s</tt> format mask.</p></div>
|
||||
<div class="paragraph"><p>The default error message for <tt>validates_inclusion_of</tt> is "<em>is not included in the list</em>".</p></div>
|
||||
<h3 id="_the_tt_validates_length_of_tt_helper">3.8. The <tt>validates_length_of</tt> helper</h3>
|
||||
<h3 id="_the_tt_validates_length_of_tt_helper">3.7. The <tt>validates_length_of</tt> helper</h3>
|
||||
<div class="paragraph"><p>This helper validates the length of your attribute’s value. It can receive a variety of different options, so you can specify length contraints in different ways.</p></div>
|
||||
<div class="listingblock">
|
||||
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
||||
|
@ -601,7 +588,7 @@ http://www.gnu.org/software/src-highlite -->
|
|||
validates_length_of <span style="color: #990000">:</span>bio<span style="color: #990000">,</span> <span style="color: #990000">:</span>too_long <span style="color: #990000">=></span> <span style="color: #FF0000">"you're writing too much. %d characters is the maximum allowed."</span>
|
||||
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
|
||||
<div class="paragraph"><p>This helper has an alias called <tt>validates_size_of</tt>, it’s the same helper with a different name. You can use it if you’d like to.</p></div>
|
||||
<h3 id="_the_tt_validates_numericality_of_tt_helper">3.9. The <tt>validates_numericality_of</tt> helper</h3>
|
||||
<h3 id="_the_tt_validates_numericality_of_tt_helper">3.8. The <tt>validates_numericality_of</tt> helper</h3>
|
||||
<div class="paragraph"><p>This helper validates that your attributes have only numeric values. By default, it will match an optional sign followed by a integral or floating point number. Using the <tt>:integer_only</tt> option set to true, you can specify that only integral numbers are allowed.</p></div>
|
||||
<div class="paragraph"><p>If you use <tt>:integer_only</tt> set to <tt>true</tt>, then it will use the <tt>/\A[+\-]?\d+\Z/+ regular expression to validate the attribute’s value. Otherwise, it will try to convert the value using +Kernel.Float</tt>.</p></div>
|
||||
<div class="listingblock">
|
||||
|
@ -614,7 +601,7 @@ http://www.gnu.org/software/src-highlite -->
|
|||
validates_numericality_of <span style="color: #990000">:</span>games_played<span style="color: #990000">,</span> <span style="color: #990000">:</span>integer_only <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
|
||||
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
|
||||
<div class="paragraph"><p>The default error message for <tt>validates_numericality_of</tt> is "<em>is not a number</em>".</p></div>
|
||||
<h3 id="_the_tt_validates_presence_of_tt_helper">3.10. The <tt>validates_presence_of</tt> helper</h3>
|
||||
<h3 id="_the_tt_validates_presence_of_tt_helper">3.9. The <tt>validates_presence_of</tt> helper</h3>
|
||||
<div class="paragraph"><p>This helper validates that the attributes are not empty. It uses the <tt>blank?</tt> method to check if the value is either <tt>nil</tt> or an empty string (if the string has only spaces, it will still be considered empty).</p></div>
|
||||
<div class="listingblock">
|
||||
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
||||
|
@ -646,11 +633,11 @@ http://www.gnu.org/software/src-highlite -->
|
|||
<td class="icon">
|
||||
<img src="./images/icons/note.png" alt="Note" />
|
||||
</td>
|
||||
<td class="content">If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true</td>
|
||||
<td class="content">If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in ⇒ [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # ⇒ true</td>
|
||||
</tr></table>
|
||||
</div>
|
||||
<div class="paragraph"><p>The default error message for <tt>validates_presence_of</tt> is "<em>can’t be empty</em>".</p></div>
|
||||
<h3 id="_the_tt_validates_uniqueness_of_tt_helper">3.11. The <tt>validates_uniqueness_of</tt> helper</h3>
|
||||
<h3 id="_the_tt_validates_uniqueness_of_tt_helper">3.10. The <tt>validates_uniqueness_of</tt> helper</h3>
|
||||
<div class="paragraph"><p>This helper validates that the attribute’s value is unique right before the object gets saved. It does not create a uniqueness constraint directly into your database, so it may happen that two different database connections create two records with the same value for a column that you wish were unique. To avoid that, you must create an unique index in your database.</p></div>
|
||||
<div class="listingblock">
|
||||
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
||||
|
@ -681,6 +668,19 @@ http://www.gnu.org/software/src-highlite -->
|
|||
validates_uniqueness_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>case_sensitive <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
|
||||
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
|
||||
<div class="paragraph"><p>The default error message for <tt>validates_uniqueness_of</tt> is "<em>has already been taken</em>".</p></div>
|
||||
<h3 id="_the_tt_validates_each_tt_helper">3.11. The <tt>validates_each</tt> helper</h3>
|
||||
<div class="paragraph"><p>This helper validates attributes against a block. It doesn’t have a predefined validation function. You should create one using a block, and every attribute passed to <tt>validates_each</tt> will be tested against it. In the following example, we don’t want names and surnames to begin with lower case.</p></div>
|
||||
<div class="listingblock">
|
||||
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
||||
by Lorenzo Bettini
|
||||
http://www.lorenzobettini.it
|
||||
http://www.gnu.org/software/src-highlite -->
|
||||
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
||||
validates_each <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>surname <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>model<span style="color: #990000">,</span> attr<span style="color: #990000">,</span> value<span style="color: #990000">|</span>
|
||||
model<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>add<span style="color: #990000">(</span>attr<span style="color: #990000">,</span> <span style="color: #FF0000">'Must start with upper case'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> value <span style="color: #990000">=~</span> <span style="color: #FF6600">/^[a-z]/</span>
|
||||
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
||||
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
|
||||
<div class="paragraph"><p>The block receives the model, the attribute’s name and the attribute’s value. If your validation fails, you can add an error message to the model, therefore making it invalid.</p></div>
|
||||
</div>
|
||||
<h2 id="_common_validation_options">4. Common validation options</h2>
|
||||
<div class="sectionbody">
|
||||
|
|
|
@ -133,21 +133,6 @@ end
|
|||
|
||||
The default error message for +validates_confirmation_of+ is "_doesn't match confirmation_"
|
||||
|
||||
=== The +validates_each+ helper
|
||||
|
||||
This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to +validates_each+ will be tested against it. In the following example, we don't want names and surnames to begin with lower case.
|
||||
|
||||
[source, ruby]
|
||||
------------------------------------------------------------------
|
||||
class Person < ActiveRecord::Base
|
||||
validates_each :name, :surname do |model, attr, value|
|
||||
model.errors.add(attr, 'Must start with upper case') if value =~ /^[a-z]/
|
||||
end
|
||||
end
|
||||
------------------------------------------------------------------
|
||||
|
||||
The block receives the model, the attribute's name and the attribute's value. If your validation fails, you can add an error message to the model, therefore making it invalid.
|
||||
|
||||
=== The +validates_exclusion_of+ helper
|
||||
|
||||
This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.
|
||||
|
@ -301,6 +286,21 @@ end
|
|||
|
||||
The default error message for +validates_uniqueness_of+ is "_has already been taken_".
|
||||
|
||||
=== The +validates_each+ helper
|
||||
|
||||
This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to +validates_each+ will be tested against it. In the following example, we don't want names and surnames to begin with lower case.
|
||||
|
||||
[source, ruby]
|
||||
------------------------------------------------------------------
|
||||
class Person < ActiveRecord::Base
|
||||
validates_each :name, :surname do |model, attr, value|
|
||||
model.errors.add(attr, 'Must start with upper case') if value =~ /^[a-z]/
|
||||
end
|
||||
end
|
||||
------------------------------------------------------------------
|
||||
|
||||
The block receives the model, the attribute's name and the attribute's value. If your validation fails, you can add an error message to the model, therefore making it invalid.
|
||||
|
||||
== Common validation options
|
||||
|
||||
There are some common options that all the validation helpers can use. Here they are, except for the +:if+ and +:unless+ options, which we'll cover right at the next topic.
|
||||
|
|
Loading…
Reference in a new issue