2

I am working on a project and need some help on where to begin. I have three pages

  • Update User
  • Create User
  • Admin User Password Change (like a Hard Reset Password for but only the admin can reset the user's password)
  • Change Password

On Create User first name, last name, username, password, and password confirmation are mandatory.

On Update User just first name, last name and username are mandatory.

On Admin User Password Change and Change Password, just password and password confirmation are mandatory.

How would you go about doing this? I don't think this is possible through models using validates_presence_of with an if because there are too many scenarios. Any help or guidance would be appreciated. Also, I am pretty new to Rails if you can't already tell.

3 Answers 3

4

You can pass conditionals to your validations:

validates :password, :confirmation => true, :presence => true
validates :first_name, :last_name, :username, :presence => true
validate :admin_user_password_change?

Of course you'd have to define what the admin_user_password_change? method would be to determine if it is an admin user changing a password.

UPDATE

The admin_user_password_change? method might be something like:

def admin_user_password_change?
  unless self.admin? && self.password.present? && self.password_confirmation.present?
    self.errors.add(:admin_password_change, "password and password_confirmation are required.")
  end
end

As for How would it communicate with the controller?, it wouldn't directly. But if any of the conditions in the method are false (e.g. self.admin? && self.password.present? && self.password_confirmation.present?), an error will be added to the instance of User and the instance won't save in the controller.

Sign up to request clarification or add additional context in comments.

8 Comments

How would the admin_user_password_change method in the Model communicate with the Controller? Basically, how do i define the admin_user_password_change?
@CDub this looks something suspicious, first thing user shouls set validates :password, confirmation: true, presence: true and then validates :first_name, :last_name, :username is sufficient for create and update. there is no need to set it again for update by excluding password field . model will not check that validation until user is try to set them nil
for checking that update is made by admin ,you have to create a virtual field for that in your model and then pass it with params to model when attempt to update . now in model you can check presence for that virtual field to validate password fields
@CDub sorry to interrupt you again but ` :on => [:create, :update]` not required at all . what other possibility left if you include both create and update. correct me if i am wrong :)
you can not access current_user in model
|
0

Setting some fields to new values doesn't unset other fields; just because you're only updating some fields in one action doesn't mean the other fields will be unset, so long as they start in a consistent state.

Just add your validations. It will work fine.

Comments

0

You can tell to your validation work only on certain cenarios only using:

The create:

validates :first_name, :last_name, :username, presence: true, on: :create

The update:

validates :password, presence: true, on: :update

Take a look at on.

For validation based on context take a look at Context Validations

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.