Reputation: 1237
In my current ROR project I am using devise pluing for validation. In my change password form validation, I am using the following code in the user
model
validates_presence_of :password, :password_confirmation
But I wants to validate is only for an action. I have a function in my user controller named update_password
. I found the that I can assign the action as follows:
validates_presence_of :password, :password_confirmation, :on => :update_password
But its not working. Even if the password and password confirmation fields are empty, the form is submitted. Can anyone help me to solve how to set the validation only for a particular action. Will be a great help
Thanks a lot
Upvotes: 0
Views: 221
Reputation: 2945
You can use :validatable option implemented in devise. Just add to your model
devise :validatable
And set validation options in your config/initializers/devise.rb
file
# ==> Configuration for :validatable
# Range for password length. Default is 6..128.
config.password_length = 6..128
# Email regex used to validate email formats. It simply asserts that
# an one (and only one) @ exists in the given string. This is mainly
# to give user feedback and not to assert the e-mail validity.
config.email_regexp = /\A[^@]+@[^@]+\z/
Another way is to use your own regexp validations. You can add to your model
validates :password, :format => { :with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed" }
After that you can call @user.valid? in your controller to check that your user instance is correct.
There are many different ways to validate your model. You can read more about validation and ActiveRecord callbacks in guides: http://guides.rubyonrails.org/active_record_validations_callbacks.html
Upvotes: 2
Reputation: 9691
If you're using devise, there's no need to check for that. It's handle for you by default.
Upvotes: 0