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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. ? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
johnmcauley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-08 10:14 UTC
Re: Validation question
I think that''s exactly what I was looking for, thank you very much... j On May 8, 10:47 am, "Jordan Elver" <jordan.el...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
johnmcauley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-08 11:45 UTC
Re: Validation question
I tried that, it doesn''t work as there are no such parameters for a validation helper. On May 8, 10:25 am, shai <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. > > ? > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---