Validation question

Hello all,

I have a controller with a couple of actions and one model.
The model is as so:

validates_presence_of :name, :email, :name
validates_presence_of :password, :password_confirmation

The first action is create and is called by the administrator.
With this action I do not want to validate the password as this is
created
automatically.

The second action allows the user change their password.
With this action I do not want to validate the email, as there is no
need.

However the problem lies in the fact that the Administrator can also
change
the name and email.
Therefore I am unable to use the :on => create or :on => update, as
password
validation is required on update for the user,
while email and name validation is required on update and create by the
administrator. Quite the pickle! Is there a way of
restricting validation to a controller/action? Therefore I can avoid
calling certain validations at certain actions?

Thanks,

j

a simple suggestion on the top of my head would be to create two
different actions for the admin / user. instead of :update, make two
identical actions, with different names such as :update_for_user &&
:update_for_admin and then just set the

validates_* to :update_for_user
and
validates_* :update_for_admin

in the correct cases.

?

I think that’s exactly what I was looking for, thank you very much…

j

This is how I did it. Sounds similar to what you need.

Model:

class Customer < ActiveRecord::Base
attr_accessor :current_password
validates_presence_of :current_password, :on => :update, :if =>
:password_change?

def change_password(attributes)
  @password_change = true
  self.attributes = attributes
  save
end

private

    def validate_on_update
        if password_change?
            errors.add(:current_password, "is incorrect") unless

encrypt(current_password) == crypted_password
end
end

    def password_change?
        @password_change
    end

end

Controller:

class AccountController < ApplicationController
def change_password
@customer = @current_customer
if request.post?
if @customer.change_password(params[:customer])
flash[:notice] = “Your password has been changed”
end
end
end
end

I tried that, it doesn’t work as there are no such parameters for a
validation helper.