heartcombo / devise

Flexible authentication solution for Rails with Warden.
http://blog.plataformatec.com.br/tag/devise/
MIT License
24.03k stars 5.55k forks source link

The email validations (uniqueness and regexp) is not working with client_side_validations #4574

Closed salmagomaa closed 7 years ago

salmagomaa commented 7 years ago

Version of Devise: 4.3.0 Version of ClientSideValidations: 9.3.3 Version of Rails: 5.1.1 User model:

class User < ApplicationRecord

include DeviseAsync

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable

end

The template form code:

<%= form_for(resource, as: resource_name,
url: registration_path(resource_name),
turboboost: true,
validate: true,
html: {id: "register-form-id"}) do |f| %>

<%= f.email_field :email, class: "form-control", validate: true %>

<% end %>

Result: On blurring the email's input field, an error only shown if it is empty but if it already existed or if it has invalid format, no error is shown. I need the client_side_validations to be shown for all cases of the email errors, any help please??

ahmedyehia commented 7 years ago

Hi, For the unique problem, it should be done using a remote validator.

For the regex or format problem, It is an issue in client side validation gem. client side validation has a method called check_conditionals responsible for checking the conditions of the validation and check if it will be added to client side or not.

It returns true if the validation condition contains changed? and Devise was checking for applying email validation using email_changed?.

But it looks like now Devise using new Rails 5 syntax in validatable module it checks for the email validation using will_save_change_to_email?.

So to fix the problem now you can add file in your initializers folder config/initializers/client_side_validations/active_model.rb and override the method like this:

# frozen_string_literal: true

require 'client_side_validations/core_ext'
require 'client_side_validations/extender'
require 'client_side_validations/active_model/conditionals'

module ClientSideValidations
  module ActiveModel
    module Validations
      include ClientSideValidations::ActiveModel::Conditionals

      def check_conditionals(attr, validator, force)
        return true if validator.options[:if] && (validator.options[:if] =~ /changed\?/ || validator.options[:if] =~ /\Awill_save_change_to/)

        result = can_force_validator?(attr, validator, force)

        if validator.options[:if]
          result &&= run_conditionals(validator.options[:if], :if)
        end

        if validator.options[:unless]
          result &&= run_conditionals(validator.options[:unless], :unless)
        end

        result
      end

    end
  end
end

And I am currently working in a PR to fix this issue in client_side_validation gem.

tegon commented 7 years ago

I'm closing this because it seems like it was fixed in the client_side_validations. Please reopen if necessary.

Thanks