0

How do I validate a password field on 'create' and 'change' only.

And skip validation if it's empty on the update page meaning that the user doesn't want to change it?

Here's part of the form

<%= semantic_form_for [:admin, @account] do |f| %>
  <%= f.semantic_errors %>

  <%= f.inputs do %>
    <%= f.input :email %>
    <%= f.input :password %>
    <%= f.input :password_confirmation, :label => 'Confirm Password' %>

I've tried the answer here

How do I exclude password fields from validation during record update? (Rails 3.0.4, Ruby 1.9.2)

but it still checks when the password field is empty on update.

Community
  • 1
  • 1
wnoveno
  • 546
  • 3
  • 9
  • 25

3 Answers3

0

If you are using devise authentication system, you might want to check this one document: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

Jackie Chan
  • 2,654
  • 6
  • 35
  • 70
0

If you want to skip validations and callbacks maybe this answer could help you...

Community
  • 1
  • 1
Dol
  • 944
  • 3
  • 10
  • 25
0

Simply do this:

validates :password, presence => true, :if => :password

This will skip at time of updation.

Hauleth
  • 22,873
  • 4
  • 61
  • 112
Aayush Khandelwal
  • 1,071
  • 7
  • 11
  • It's throwing an error (front end) since the password field is blank on update. – wnoveno Sep 26 '12 at 23:31
  • in all rails application or even in any application update password anad update fields are done on seprate pages so it will be better u do it on seprate pages means seprate functions – Aayush Khandelwal Oct 01 '12 at 11:39