Custom validation for two fields with same validation but slightly different

Magicmarkker

Here is the scope that causes code smell:

validates :email, uniqueness: { scope: :client_id }, if: Proc.new {|u| u.active? and !u.email.blank? }
validates :companyemail, uniqueness: { scope: :client_id }, if: Proc.new {|u| u.active? and !u.companyemail.blank? }

Here's what I've tried, but it causes my tests to fail saying the email fields have already been taken:

class EmailValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors[attribute] << 'has already been taken' if record.active? and !attribute.blank?
  end
end

validates :email, :companyemail, uniqueness: { scope: :client_id }, email: true
vee

You could group the two lines using with_options:

Example:

with_options uniqueness: { scope: :client_id }, if: Proc.new { |u| u.active? && u.email } do 
  validates :email
  validates :companyemail
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Custom form validation directive to compare two fields

From Dev

Custom validation for custom fields in React

From Dev

How to reuse custom validation logic in multiple fields from the same Domain

From Dev

How to reuse custom validation logic in multiple fields from the same Domain

From Dev

Joi validation schema - two fields must not have the same value

From Dev

jQuery remote validation not working when two fields having same id

From Dev

Unique validation across two fields

From Dev

two fields validation with laravel 5

From Dev

Django - custom validation for different users

From Dev

Validation of two forms on same page

From Dev

Using two different domains for validation

From Java

Django REST Framework custom fields validation

From Dev

Mongoose custom validation of several fields on update

From Dev

Rails custom validation for Postgres Json fields

From Dev

mongoose custom validation using 2 fields

From Dev

Form mapping that handles optional fields with custom validation

From Dev

Spring MVC - validation for comparing two fields

From Dev

Laravel Validation one of two fields must be filled

From Dev

Using is_unique validation rule with two fields?

From Dev

form validation check if two fields are equal

From Dev

At least one of two fields must be completed validation

From Dev

Using is_unique validation rule with two fields?

From Dev

validation of two or more number fields in form

From Dev

Spring MVC - validation for comparing two fields

From Dev

how to check validation of csv file with two fields

From Dev

jQuery Validation Plugin: same form, different validation submitHandler

From Dev

jQuery Validation Plugin: same form, different validation submitHandler

From Dev

Same Remote Validation for 2 different properties in a model

From Dev

ASP validation control on same form but different divs

Related Related

  1. 1

    Custom form validation directive to compare two fields

  2. 2

    Custom validation for custom fields in React

  3. 3

    How to reuse custom validation logic in multiple fields from the same Domain

  4. 4

    How to reuse custom validation logic in multiple fields from the same Domain

  5. 5

    Joi validation schema - two fields must not have the same value

  6. 6

    jQuery remote validation not working when two fields having same id

  7. 7

    Unique validation across two fields

  8. 8

    two fields validation with laravel 5

  9. 9

    Django - custom validation for different users

  10. 10

    Validation of two forms on same page

  11. 11

    Using two different domains for validation

  12. 12

    Django REST Framework custom fields validation

  13. 13

    Mongoose custom validation of several fields on update

  14. 14

    Rails custom validation for Postgres Json fields

  15. 15

    mongoose custom validation using 2 fields

  16. 16

    Form mapping that handles optional fields with custom validation

  17. 17

    Spring MVC - validation for comparing two fields

  18. 18

    Laravel Validation one of two fields must be filled

  19. 19

    Using is_unique validation rule with two fields?

  20. 20

    form validation check if two fields are equal

  21. 21

    At least one of two fields must be completed validation

  22. 22

    Using is_unique validation rule with two fields?

  23. 23

    validation of two or more number fields in form

  24. 24

    Spring MVC - validation for comparing two fields

  25. 25

    how to check validation of csv file with two fields

  26. 26

    jQuery Validation Plugin: same form, different validation submitHandler

  27. 27

    jQuery Validation Plugin: same form, different validation submitHandler

  28. 28

    Same Remote Validation for 2 different properties in a model

  29. 29

    ASP validation control on same form but different divs

HotTag

Archive