aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorVishnu Atrai <vishnu.atrai@gmail.com>2011-08-05 20:15:48 +0530
committerXavier Noria <fxn@hashref.com>2011-08-13 16:22:35 -0700
commit54cd73e20d5fe2c4168e04f9525b8793e9e3c64c (patch)
treec4a23b3d51139056588496e5e75ee180a93ba16b /railties
parent56efdbc6260f49fdf8d82d8557f233a7df3beafa (diff)
downloadrails-54cd73e20d5fe2c4168e04f9525b8793e9e3c64c.tar.gz
rails-54cd73e20d5fe2c4168e04f9525b8793e9e3c64c.tar.bz2
rails-54cd73e20d5fe2c4168e04f9525b8793e9e3c64c.zip
ActiveModel::Validations basic guide
Diffstat (limited to 'railties')
-rw-r--r--railties/guides/source/active_model_basics.textile24
1 files changed, 24 insertions, 0 deletions
diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile
index 404cc71c50..f3a2b2edbc 100644
--- a/railties/guides/source/active_model_basics.textile
+++ b/railties/guides/source/active_model_basics.textile
@@ -176,6 +176,30 @@ person.first_name_change #=> ["First Name", "First Name 1"]
person.last_name_change #=> nil
</ruby>
+h4. Validations
+
+Validations module adds the ability to class objects to validate them in Active Record style.
+
+<ruby>
+class Person
+ include ActiveModel::Validations
+
+ attr_accessor :name, :email
+
+ validates :name, :presence => true
+ validates_format_of :email, :with => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i
+
+end
+
+person = Person.new
+person.valid? #=> false
+person.name = 'vishnu'
+person.email = 'me'
+person.valid? #=> false
+person.email = 'me@vishnuatrai.com'
+person.valid? #=> true
+</ruby>
+
h3. Changelog
* August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw