Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
salmagomaa opened this issue Jul 2, 2017 · 2 comments

Comments

@salmagomaa
Copy link

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
Copy link

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.

tagliala pushed a commit to DavyJonesLocker/client_side_validations that referenced this issue Jul 15, 2017
This commit reflects changes in ActiveRecord::Dirty API version 5.1

Ref: #710, heartcombo/devise#4574, rails/rails#25337
@tegon
Copy link
Member

tegon commented Nov 30, 2017

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

Thanks

@tegon tegon closed this as completed Nov 30, 2017
jeremycraney added a commit to jeremycraney/client_side_validations that referenced this issue Dec 28, 2021
This commit reflects changes in ActiveRecord::Dirty API version 5.1

Ref: #710, heartcombo/devise#4574, rails/rails#25337
LeisuPan added a commit to LeisuPan/client_side_validations that referenced this issue Jul 16, 2022
This commit reflects changes in ActiveRecord::Dirty API version 5.1

Ref: #710, heartcombo/devise#4574, rails/rails#25337
richardstewart0213 added a commit to richardstewart0213/rubyonrails-client_side_validations that referenced this issue Nov 4, 2022
This commit reflects changes in ActiveRecord::Dirty API version 5.1

Ref: #710, heartcombo/devise#4574, rails/rails#25337
mikejohn857 added a commit to mikejohn857/client_side_validations-ruby that referenced this issue Nov 25, 2022
This commit reflects changes in ActiveRecord::Dirty API version 5.1

Ref: #710, heartcombo/devise#4574, rails/rails#25337
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants