0

I'm trying to use update_attributes to update my user object, but even though I don't pass in a password attribute in the params I send to update_attributes, I still get the password blank error.

I have two pages, one for updating user info except password and one for just updating password. How do I force the password validations to trigger on blank/nil on one form, but not the other?

validates :password, length: {minimum: 8}, format: {with: VALID_PASSWORD_REGEX}
Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47
Derek
  • 11,980
  • 26
  • 103
  • 162

1 Answers1

0

Have you tried restricting your presence validation to the create action?

validates_presence_of :password, :on => :create
validates             :password,
                      :length => {
                        :minimum => 8
                      },
                      :format => {
                        :with => VALID_PASSWORD_REGEX
                      },
                      :allow_blank => true
Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47
  • I also need to validate passwords when a user changes it – Derek Oct 19 '13 at 01:11
  • Yup, that's only skipping the presence validation. All others would still apply. – Marcelo De Polli Oct 19 '13 at 01:13
  • but in the case the user doesn't put anything when changing, presence is needed to catch that right? Also, I have a length and a regex that would give me an error on a blank/nil password, right? – Derek Oct 19 '13 at 01:16
  • That's where you use `:allow_blank => true`, for instance: http://stackoverflow.com/questions/4441882/validating-min-and-max-length-of-a-string-but-allowing-it-to-be-blank – Marcelo De Polli Oct 19 '13 at 01:19
  • Edited my question to include the other validations. – Marcelo De Polli Oct 19 '13 at 01:24
  • I don't want the password update form to be good when the user updates with blank new passwords. This causes the form to go through on password update with blank password fields. – Derek Oct 19 '13 at 01:26
  • I'm not sure what you mean by "updates with blank new passwords". If the user doesn't fill up the password field, it doesn't even gets included in the params. – Marcelo De Polli Oct 19 '13 at 01:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39536/discussion-between-depa-and-derek) – Marcelo De Polli Oct 19 '13 at 01:29
  • right, the user.valid? should equal false when a user updates with blank passwords. But these validations cause user.valid? to be true. – Derek Oct 19 '13 at 01:30