Validates :on 2


#1

Hello, I need to have this for :create and :login how do I do so
validates_presence_of :login, :password, :password_confirmation,
:user_name, :email, :on => :create

I do need it to be off in a number of places though


#2

Anyone know how?


#3

bump


#4

Mohammad wrote:

Hello, I need to have this for :create and :login how do I do so
validates_presence_of :login, :password, :password_confirmation,
:user_name, :email, :on => :create

I do need it to be off in a number of places though

You should be able to use the validate_on_create method for this, or
alternatively just list the requirements one after the other with the
:on parameter (though that may be venturing into the region of
unmaintainable code).

See the following docs:
http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html

Example:

def validate_on_create
errors.add_on_empty %w( login password password_confirmation
user_name email )
end

To selectively turn it off, you can apply whatever conditional
structures you’d like to use within the validate_on_create method.
Alternatively, I’ve seen the following setup used:

validates_presence_of :login
validates_presence_of :password, :if => :password_required?
validates_presence_of :password_confirmation, :if =>
:password_required?

Where :password_required is a protected method in the model which
determines if the password is actually needed at that time.

def password_required?
crypted_password.blank? or not password.blank?
end

The latter two examples are directly pulled from Geoffrey Grosenbach’s
Gullery app. Check it out:
http://www.nubyonrails.com/pages/gullery