Permalink
Browse files

ActiveModel::Validations basic guide

  • Loading branch information...
1 parent 56efdbc commit 54cd73e20d5fe2c4168e04f9525b8793e9e3c64c @vishnuatrai vishnuatrai committed with fxn Aug 5, 2011
Showing with 24 additions and 0 deletions.
  1. +24 −0 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

0 comments on commit 54cd73e

Please sign in to comment.