www.SmarteGuru.com
  Home | Blogs | Recipe | Find a Friend | Discussion Board | Resources | Developers Area | Articles | Health |  Login | Register Now 

Validate attributes plugin with ruby on rails

validate_attributes is a really simple plugin to validate specific attributes of a model unlike the method valid? which collectively validates all the attributes. This plugin provides the functionality of validating specific attribute(s) and skipping others’ validations and vice versa.

Installation
: svn://vinsol.com/plugins/sur/validate_attributes

Following are the ActiveRecord instance methods that will be available after installing this plugin

1.) validate_attributes
2.) validate_attributes_and_save

Usage Examples:

lets assume a User model with some validations as…

class User < ActiveRecord::Base
validates_presence_of :name
validates_length_of :name, :minimum => 5
validates_presence_of :email
validates_presence_of :address
end


1.) validate_attributes
Parameters:
We can specify attributes as parameters in the form of :only or :except.
If attributes’ array is given with :only, the method will validate only those attributes.
If given with :except, it will validate all attributes but skip the specified.

@user = User.new
@user.validate_attributes(:only => ["name"]) # => false
@user.errors.full_messages # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
@user.name = “sur”
@user.validate_attributes(:only => ["name"]) # => false
@user.errors.full_messages # => ["Name is too short (minimum is 5 characters)"]
@user.validate_attributes(:only => ["name", "email"]) # => false
@user.errors.full_messages # => ["Name is too short (minimum is 5 characters)", "Email can't be blank"]
@user.validate_attributes # => false
@user.errors.full_messages # => ["Name is too short (minimum is 5 characters)", "Email can't be blank", "Address can't be blank"]
@user = User.new
@user.validate_attributes(:except => ["name", "email"]) # => false
@user.errors.full_messages # => ["Address can't be blank"]
@user = User.new
@user.validate_attributes(:only => ["address"]) # => false
@user.errors.full_messages # => ["Address can't be blank"]

2.) validate_attributes_and_save

Parameters:
Same as the method validate_attributes

This method will save the object if the function validate_attributes returns true, and will return true if object gets saved
else return false.

@user = User.new
@user.validate_attributes_and_save(:only => [:address]) # => false
@user.new_record? # => true

@user.address = "http://smarteguru.com"
@user.validate_attributes_and_save(:only => [:address]) # => true
@user.new_record? # => false

Social Bookmarks: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Webnews
  • Y!GG
  • Google Bookmarks
  • SEOigg
  • YahooMyWeb
  • Live-MSN
  • DZone
  • Facebook
  • Technorati
  • Ask
  • Furl
  • Spurl
  • Webbrille

Related Posts

Tags: , , ,

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Viewed: 182 views

Leave a Reply

Twitter Users!
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.

Home - About Us - Help - Terms and Conditions - Site Map - Link to Us - Resources - Contact Us
Google Rank Calculator | Suggest developer resource | Suggest Article
All rights reserved © 2007 SmarteGuru.com.