Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8848957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:31:36+00:00 2026-06-14T12:31:36+00:00

In my applications, there are multiple custom validators that I keep in app/validators and

  • 0

In my applications, there are multiple custom validators that I keep in app/validators and then call in multiple models.

Mostly I do this for regex based validations, things like email, mobiles, etc basically custom data strings that I need to be in a particular format.

For example:

class EmailValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
      record.errors[attribute] << (options[:message] || "is not a valid email format")
    end
  end
end

class IpValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    unless value =~ /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/
      record.errors[attribute] << (options[:message] || "is not a valid IP format")
    end
  end
end

The downside of this method is that I have a lot of extra files with pretty much the same code which can get annoying in larger applications.

Is there a better way (more DRY) to define regex based validations such that they are reusable across models?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:31:38+00:00Added an answer on June 14, 2026 at 12:31 pm

    You can make a base class for regexp validation:

    class RegexValidator < ActiveModel::EachValidator
      def regex_validate_each(regex, err_msg, record, attribute, value)
        unless value =~ regex
          record.errors[attribute] << (options[:message] || err_msg)
        end
      end
    end
    

    Then subclass it like:

    class EmailValidator < RegexValidator
      def validate_each(record, attribute, value)
        regex_validate_each(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, "is not a valid IP format", record, attribute, value)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is staging server with multiple .NET web applications deployed. Is there any way
Is there a way to apply consistent visual appearance for multiple Windows Forms applications?
In an application that has multiple webviews, is there any way to have the
We all know that most applications out there assume class names to follow the
I have a non document-based Core Data application. There's an NSTreeController that manages a
In this hypothetical scenario there is an ASP.NET 4 web application that simultaneously aggregates
I have an application that interacts multiple databases and some custom services. For some
I have rails application that connects to multiple databases. I wrote custom rake task
I have an application where there are multiple processes. They share the reading and
I have an rails 3 application where there are multiple registrations (diagnosis, patient, laboratory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.