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

[BE]Signup validations #70

Merged
merged 10 commits into from
Jan 13, 2022
14 changes: 7 additions & 7 deletions app/helpers/users/registrations_helper.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# frozen_string_literal: true

module Users::RegistrationsHelper
def error_message_on(object, method)
return unless object.respond_to?(:errors) && object.errors.include?(method)
error = field_error(object, method)
def error_message_on(resource, attribute)
return unless resource.respond_to?(:errors) && resource.errors.include?(attribute)
error = field_error(resource, attribute)

content_tag(:div, error, class: "tracking-wider block text-xs font-semibold text-red-600")
end

def error_message_class(object, method)
if object.respond_to?(:errors) && object.errors.include?(method)
def error_message_class(resource, attribute)
if resource.respond_to?(:errors) && resource.errors.include?(attribute)
"rounded tracking-wider appearance-none border border-red-600 block w-full px-3 py-2 bg-miru-gray-100 h-8 shadow-sm font-semibold text-xs text-miru-dark-purple-1000 focus:outline-none focus:ring-red-600 focus:border-red-600 sm:text-base"
else
"rounded tracking-wider appearance-none border border-gray-100 block w-full px-3 py-2 bg-miru-gray-100 h-8 shadow-sm font-semibold text-xs text-miru-dark-purple-1000 focus:outline-none focus:ring-miru-gray-1000 focus:border-miru-gray-1000 sm:text-base"
end
end

private
def field_error(object, method)
object.errors[method].first
def field_error(resource, attribute)
resource.errors[attribute].first
end
end
5 changes: 3 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ class User < ApplicationRecord

validates :first_name, :last_name,
presence: true,
format: { with: /\A[a-zA-Z]+\z/, message: "First and last name must only contain letters" }
format: { with: /\A[a-zA-Z]+\z/ }
validates :first_name, :last_name, length: { maximum: 50 }
validates :email, format: { with: /^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, multiline: true }

# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:trackable, :confirmable

validates :email, format: { with: /^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, multiline: true }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move all the validates in one place

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

@keshavbiswa keshavbiswa Jan 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can replace the regex with devise's default regex inside devise.rb config.email_regexp. Either replace the default value with this or use the default value from there itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks for the tip @keshavbiswa . I was actually looking to override devise's default email validation since it's weaker and validates strings such as foo@bar.


after_create :assign_default_role

private
Expand Down
4 changes: 3 additions & 1 deletion config/locales/devise.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ en:
confirmation: "Passwords dont' match"
first_name:
blank: "First and last name can't be blank"
invalid: "First and last name must only contain letters"
last_name:
blank: "First and last name can't be blank"
invalid: "First and last name must only contain letters"
email:
invalid: "Invalid Email ID"
blank: "Email can't be blank"
invalid: "Invalid Email ID"
taken: "Email ID already exists"