Rails 5.2.2
Ruby 2.6
cient_side_vlidations gem version 13.1.0
I did:
rails g client_side_validations:install
I uncommented the following, in config/initializers/client_side_validations.rb:
ActionView::Base.field_error_proc = proc do |html_tag, instance|
if html_tag =~ /^<label/
%(<div class="field_with_errors">#{html_tag}</div>).html_safe
else
%(<div class="field_with_errors">#{html_tag}<label for="#{instance.send(:tag_id)}" class="message">#{instance.error_message.first}</label></div>).html_safe
end
end
Here's what my app/assets/javascript/application.js looks like:
//= require jquery3
//= require popper
//= require bootstrap-sprockets
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require rails.validations
//= require_tree .
I created a folder: app/validators, and added the following two files:
email_validator.rb:
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attr_name, value)
unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
record.errors.add(attr_name, :email, options.merge(value: value))
end
end
end
# This allows us to assign the validator in the model
module ActiveModel::Validations::HelperMethods
def validates_email(*attr_names)
validates_with EmailValidator, _merge_attributes(attr_names)
end
end
and password_validator.rb:
class PasswordValidator < ActiveModel::EachValidator
def validate_each(record, attr_name, value)
unless value =~ /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/i
record.errors.add(attr_name, :password, options.merge(value: value))
end
end
end
# This allows us to assign the validator in the model
module ActiveModel::Validations::HelperMethods
def validates_password(*attr_names)
validates_with PasswordValidator, _merge_attributes(attr_names)
end
end
In my app/models/user.rb, I have:
class User < ApplicationRecord
include Shared
before_create :generate_model_id_and_slug
validates_email :email # cient side validation of email
validates_password :password #client side validation of password
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:confirmable, :lockable, :timeoutable, :trackable and :omniauthable
validates :email, presence: true
validates :phone, presence: true
validates :password, presence: true
end
I also have the following file in app/assets/javascripts: rails.validations.customValidators.js:
// The validator variable is a JSON Object
// The selector variable is a jQuery Object
window.ClientSideValidations.validators.local['email'] = function(element, options) {
// Your validator code goes in here
if (!/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i.test(element.val())) {
// When the value fails to pass validation you need to return the error message.
// It can be derived from validator.message
return options.message;
}
}
// The validator variable is a JSON Object
// The selector variable is a jQuery Object
window.ClientSideValidations.validators.local['password'] = function(element, options) {
// Your validator code goes in here
if (!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/i.test(element.val())) {
// When the value fails to pass validation you need to return the error message.
// It can be derived from validator.message
return options.message;
}
}
I am not finished with the application yet, but wanted to generate a migration, as follows:
rails g migration add_last_seen_at_to_users last_seen_at:datetime
But, I am getting the following error message:
/Users/joseph/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/activerecord-5.2.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `validates_email' for User (call 'User.connection' to establish a connection):Class
Did you mean? validates_each (NoMethodError)`
I did:
I uncommented the following, in config/initializers/client_side_validations.rb:
Here's what my app/assets/javascript/application.js looks like:
I created a folder: app/validators, and added the following two files:
email_validator.rb:
and password_validator.rb:
In my app/models/user.rb, I have:
I also have the following file in app/assets/javascripts: rails.validations.customValidators.js:
I am not finished with the application yet, but wanted to generate a migration, as follows:
But, I am getting the following error message: