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 :o nly or :except.
If attributes’ array is given with :o nly, 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

Related Posts

Tags: , , ,


Viewed: 187 views

Leave a Reply

Comment moderation is enabled. Your comment may take some time to appear.

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.