From 54cd73e20d5fe2c4168e04f9525b8793e9e3c64c Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Fri, 5 Aug 2011 20:15:48 +0530 Subject: ActiveModel::Validations basic guide --- railties/guides/source/active_model_basics.textile | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'railties') 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 +h4. Validations + +Validations module adds the ability to class objects to validate them in Active Record style. + + +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 + + h3. Changelog * August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw -- cgit v1.2.3