# Validation question

**URL:** <https://www.ruby-forum.com/t/validation-question/103606>\
**Category:** Rails\
**Created:** [May 8, 2007, 11:16am UTC](https://www.ruby-forum.com/t/validation-question/103606 "2007-05-08T11:16:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_McAuley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@John\_McAuley](https://www.ruby-forum.com/u/John_McAuley)\
**Post date:** [May 8, 2007, 11:16am UTC](https://www.ruby-forum.com/t/validation-question/103606/1 "2007-05-08T11:16:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John\_McAuley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@John\_McAuley](https://www.ruby-forum.com/u/John_McAuley)\
**Post date:** [May 8, 2007, 11:25am UTC](https://www.ruby-forum.com/t/validation-question/103606/2 "2007-05-08T11:25:47Z")

</div>

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.

?

---

<div class="post-metadata">

**Author:** ![John\_McAuley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@John\_McAuley](https://www.ruby-forum.com/u/John_McAuley)\
**Post date:** [May 8, 2007, 12:14pm UTC](https://www.ruby-forum.com/t/validation-question/103606/3 "2007-05-08T12:14:58Z")

</div>

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

j

---

<div class="post-metadata">

**Author:** ![John\_McAuley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@John\_McAuley](https://www.ruby-forum.com/u/John_McAuley)\
**Post date:** [May 8, 2007, 11:48am UTC](https://www.ruby-forum.com/t/validation-question/103606/4 "2007-05-08T11:48:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John\_McAuley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@John\_McAuley](https://www.ruby-forum.com/u/John_McAuley)\
**Post date:** [May 8, 2007, 1:46pm UTC](https://www.ruby-forum.com/t/validation-question/103606/5 "2007-05-08T13:46:10Z")

</div>

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