All Projects → mdespuits → Validates_formatting_of

mdespuits / Validates_formatting_of

Licence: mit
Common Rails validations wrapped in a gem.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Validates formatting of

Devise masquerade
Extension for devise, enable login as functionality. Add link to the masquerade_path(resource) and use it.
Stars: ✭ 380 (+317.58%)
Mutual labels:  rails, gem
Octicons
A scalable set of icons handcrafted with <3 by GitHub
Stars: ✭ 7,039 (+7635.16%)
Mutual labels:  rails, gem
Motion
Reactive frontend UI components for Rails in pure Ruby
Stars: ✭ 498 (+447.25%)
Mutual labels:  rails, gem
Pager Api
Easy API pagination for Rails
Stars: ✭ 86 (-5.49%)
Mutual labels:  rails, gem
Administrate Field Belongs to search
Plugin that adds search capabilities to belongs_to associations for Administrate
Stars: ✭ 29 (-68.13%)
Mutual labels:  rails, gem
Milia
Easy multi-tenanting for Rails5 (or Rails4) + Devise
Stars: ✭ 326 (+258.24%)
Mutual labels:  rails, gem
Materialize Sass
Materializecss rubygem for Rails Asset Pipeline / Sprockets
Stars: ✭ 785 (+762.64%)
Mutual labels:  rails, gem
Flexirest
Flexirest - The really flexible REST API client for Ruby
Stars: ✭ 188 (+106.59%)
Mutual labels:  rails, gem
Adminpanel
This gem has the goal to act as the administration panel for your resources, so you can focus only on the public part of your app
Stars: ✭ 7 (-92.31%)
Mutual labels:  rails, gem
Datagrid
Gem to create tables grids with sortable columns and filters
Stars: ✭ 921 (+912.09%)
Mutual labels:  rails, gem
Material icons
A simple Rails wrapper for Google Material Icons
Stars: ✭ 266 (+192.31%)
Mutual labels:  rails, gem
Tabler Rubygem
Rubygem for https://tabler.github.io
Stars: ✭ 77 (-15.38%)
Mutual labels:  rails, gem
Forest Rails
🌱 Rails Liana for Forest Admin
Stars: ✭ 247 (+171.43%)
Mutual labels:  rails, gem
Algoliasearch Rails
AlgoliaSearch integration to your favorite ORM
Stars: ✭ 352 (+286.81%)
Mutual labels:  rails, gem
Arctic admin
Responsive Theme for ActiveAdmin
Stars: ✭ 201 (+120.88%)
Mutual labels:  rails, gem
Yt
The reliable YouTube API Ruby client
Stars: ✭ 674 (+640.66%)
Mutual labels:  rails, gem
Spina
Spina CMS
Stars: ✭ 1,926 (+2016.48%)
Mutual labels:  rails, gem
Ordinare
Ordinare sorts gems in your Gemfile alphabetically
Stars: ✭ 153 (+68.13%)
Mutual labels:  rails, gem
Bh
Bootstrap Helpers for Ruby
Stars: ✭ 834 (+816.48%)
Mutual labels:  rails, gem
Render async
render_async lets you include pages asynchronously with AJAX
Stars: ✭ 974 (+970.33%)
Mutual labels:  rails, gem

Build Status Dependency Status Code Climate

Validates Formatting Of

The validates_formatting_of gem adds several convenient methods to validate things such as emails, urls, and phone numbers in a Rails application.

Supported Ruby Versions

  • 1.9.3
  • 2.0.0
  • 2.1.0
  • 2.2.0

Installation

To install validates_formatting_of, add the following to your Gemfile:

gem 'validates_formatting_of'

Then bundle install:

bundle install

Usage

Using validates_formatting_of is as simple as using Rails' built-in validation methods in models.

class User < ActiveRecord::Base
  validates_formatting_of :email
end

If the name of the column is identical to the validation method you would use, it auto-selects that validation method. Thus, this is a shortcut for the following:

class User < ActiveRecord::Base
  validates_formatting_of :email, :using => :email
end

This call will ensure that the user-provided email is a valid email. This way, you will not need to find or write your own regex to validate. All of that logic is contained within validates_formatting_of.

Rails validation options still available

You can still add the following options when using validates_formatting_of:

  • :allow_nil
  • :allow_blank
  • :on
  • :if
  • :unless
  • You can also specify the :message option to use a custom validation message.

Share your Validations

Say, for example, you have identical plain-old regex validations for different columns or even on different models entirely. You can easily alleviate this problem by simply adding the validation(s) in an initializer file.

While very unrealistic, these examples should serve their purpose in demonstrating this ability.

# config/initializers/validates_formatting_of.rb
ValidatesFormattingOf::Method.add :loweralpha, /[a-z]/, "must be lowercase and no spaces"
ValidatesFormattingOf::Method.add :upperalpha, /[A-Z]/, "must be uppercase and no spaces"
ValidatesFormattingOf::Method.add :weak_password, /[a-zA-Z0-9]{8,}/, "must contain only letters and numbers and be at least 8 characters long".

Keep in mind, since this gem is only ActiveModel-dependent, you should be able to do this in whatever non-Rails application classes you have that include ActiveModel::Validations in some way (and that extend the ValidatesFormattingOf::ModelAdditons module).

Built-in Validations

validates_formatting_of has the following built-in validations:

class ExampleModel < ActiveRecord::Base
  validates_formatting_of :email, :using => :email      # Email
  validates_formatting_of :first_name, :using => :alpha # Letters
  validates_formatting_of :website, :using => :url      # URLs
  validates_formatting_of :text, :using => :alphanum    # Alpha-numeric
  validates_formatting_of :zipcode, :using => :us_zip   # US Zip Code
  validates_formatting_of :phone, :using => :us_phone   # US Phone Numbers
  validates_formatting_of :ip, :using => :ip_address_v4 # IPv4
  validates_formatting_of :ssn, :using => :ssn          # Social Security Numbers
  validates_formatting_of :color, :using => :hex_color  # Hexadecimal Colors
  validates_formatting_of :amount, :using => :dollars   # Dollars

  # Credit Card (Visa, Mastercard, Discover, and American Express)
  validates_formatting_of :cc, :using => :credit_card
end

Note: If you use client_side_validations then you need to use the following validation method:

class User < ActiveRecord::Base
  validates_formatting_of :email, :using => :simple_email
end

Plain Old Ruby Objects

If you are not using ActiveRecord or want to use it on some other type of object, this is the least you need to get validations running.

class User
  include ActiveModel::Validations
  extend ValidatesFormattingOf::ModelAdditions

  attr_accessor :email

  validates_formatting_of :email, :using => :email
end

Over-writable

If, for any reason, you want to overwrite the regex specified in the gem, you can using the :with option just like in ActiveModel's built-in validates_format_of.

class Person < ActiveRecord::Base
  validates_formatting_of :first_name, :with => /[A-Z]/i
end

Development and Contribution

To add a simple regex validation, all that is necessary is to add a single line to the ValidatesFormattingOf::Method module in the lib/method.rb file (with a line or two of documentation explaining it's use).

For example:

module ValidatesFormattingOf::Method
  # This :validation_name method is for example purposes only.
  # You can use this in production, but it would require a value of 'example regex' to pass.
  add :validation_name, %r{example regex}iux, "is a message for validation method :validation_name"
end
  1. Fork it
  2. Make it awesome (with passing tests)
  3. Create a new Pull Request.

Have Ideas?

Do you use a particular pattern on a regular basis that isn't here or you would like to contribute? For now, create a new issue in the issue tracker. I would be more than happy to consider adding it to the project.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].