-
Notifications
You must be signed in to change notification settings - Fork 403
Devise
Devise's validation strategies add conditionals to the validators
By design ClientSideValidations
will filter out all validators with conditionals. However, you can override this behavior on each input.
<%= f.text_field :password, :validate => true -%>
<%= f.text_field :password, :validate => { :presence => true } -%>
In the first case all validators for the :password
attribute will be forced to obey any conditionals. In the second case only the :presence
validator will obey conditionals.
"Obeying" conditionals means the conditional will be evaluated at the point the form is generated.
When using Devise you might notice that if you are editing a user's password the client side validation isn't working for the confirmation field, but only is when creating a new user.
The expected behaviour is that any form with :password_confirmation
in it shows an error when the two password fields don't match up. This only works in the registration form of Devise however, even when you add :validate => true
to the field. (Which you should have done either way, otherwise this solution won't work either).
This is because, by default, Devise only adds the validates_confirmation_of :password
on the 'User' model (or any other model you chose as the authentication model) in the case of a :create
. When updating we are of course editing rather than creating.
This looks as follows in Devise's source:
validates_confirmation_of :password, :only => :create
To fix this inconvenience you will have to add the following line to your User model (again; or other model you chose):
validates_confirmation_of :password
Here is an example of what that could look like:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :token_authenticatable, :confirmable, :lockable
# Setup accessible (or protected) attributes for your model
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me
validates_confirmation_of :password
end
Make sure to put the line at the end in order for it not to be overridden by any of Devise's own settings.
Note: Now your user confirmation will be checked every time you set User.password_confirmation
.
For the default Rails form helpers:
<%= form_for @user, :validate => true do |u| %>
<%= u.text_field :email, :validate => true %>
<% end %>
Please note the different sytnax if you are using SimpleForm or Formtastic.
In addition to changing your form view, the default regular expression for matching emails that Devise
uses is not compatible with Javascript. To fix the problem, you will have to roll your own regular expression for matching emails like this (notice the :email_regexp option that has been added to the default model):
# app/models/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable, :email_regexp => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
For using client_side_validations with devise forms displayed by alternate controllers, such as on your home page, see here.
see more information here : http://stackoverflow.com/questions/6012428/rails-3-client-side-validations-gem-and-devise-password-validations/6040520#6040520