diff options
author | CassioMarques <cassiommc@gmail.com> | 2008-11-08 10:32:52 -0200 |
---|---|---|
committer | CassioMarques <cassiommc@gmail.com> | 2008-11-09 19:31:47 -0200 |
commit | 172f3ce35084ac7ae9f37e8cecea00909560a70d (patch) | |
tree | 9e2f68394dac58cac84a50bc837b19f26a40e930 /railties/doc | |
parent | fb30b82bd0a0f2dc9974aea1fe2e32fb6d2b41a7 (diff) | |
download | rails-172f3ce35084ac7ae9f37e8cecea00909560a70d.tar.gz rails-172f3ce35084ac7ae9f37e8cecea00909560a70d.tar.bz2 rails-172f3ce35084ac7ae9f37e8cecea00909560a70d.zip |
Added documentation for validates_each
Diffstat (limited to 'railties/doc')
-rw-r--r-- | railties/doc/guides/html/activerecord_validations_callbacks.html | 13 | ||||
-rw-r--r-- | railties/doc/guides/source/activerecord_validations_callbacks.txt | 13 |
2 files changed, 26 insertions, 0 deletions
diff --git a/railties/doc/guides/html/activerecord_validations_callbacks.html b/railties/doc/guides/html/activerecord_validations_callbacks.html index a16fbaad05..30e8a82889 100644 --- a/railties/doc/guides/html/activerecord_validations_callbacks.html +++ b/railties/doc/guides/html/activerecord_validations_callbacks.html @@ -429,6 +429,19 @@ http://www.gnu.org/software/src-highlite --> </tt></pre></div></div>
<div class="para"><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="para"><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="para"><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_format_of_tt_helper">3.6. The <tt>validates_format_of</tt> helper</h3>
<h3 id="_the_tt_validates_inclusion_of_tt_helper">3.7. The <tt>validates_inclusion_of</tt> helper</h3>
diff --git a/railties/doc/guides/source/activerecord_validations_callbacks.txt b/railties/doc/guides/source/activerecord_validations_callbacks.txt index f05628302d..082832b809 100644 --- a/railties/doc/guides/source/activerecord_validations_callbacks.txt +++ b/railties/doc/guides/source/activerecord_validations_callbacks.txt @@ -133,6 +133,19 @@ The default error message for +validates_confirmation_of+ is "_doesn't match con === 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 === The +validates_format_of+ helper |