MoreValidators
MoreValidators is a Rails gem that provides a series of extra validators.
The latest version of this library provides the following validation methods:
-
validates_as_cep: brazilian postal code validation;
-
validates_as_cnpj: brazilian CNPJ validation;
-
validates_as_cpf: brazilizan CPF validation;
-
validates_as_email: email address validation;
-
validates_as_phonenumber_br: brazilian phone number validation;
-
validates_as_uf_br: brazilian state validation;
-
validates_as_website: website URL validation;
-
validates_as_age: age validation for date fields.
Contact us if you have any questions: [email protected]
Latest version:
Version: 0.2.1
Installation:
The latest version of MoreValidators is compatible with Rails 3 ONLY. However, it’s possible to use this gem with Rails 2; if that’s what you need, follow the installation instructions on the Rails 2 section below.
Rails 3:
You can install the latest version of MoreValidators with:
gem install more_validators You can add it to your Gemfile: gem ‘more_validators’
Rails 2:
You can install MoreValidators with:
gem install more_validators –version=0.1.3 You can add it to your config/environment.rb: config.gem ‘more_validators’, :lib => ‘more_validators’, :version => ‘0.1.3’
Usage:
In your model file do something like:
class MyClass < ActiveRecord::Base
# Email validation validates_as_email :email # Brazilian postal code validation validates_as_cep :cep, :allow_blank => true # Brazilian phone number validation validates_as_phonenumber_br :phonenumber, :allow_nil => true # CPF validation validates_as_cpf :cpf # CNPJ validation validates_as_cnpj :cnpj :allow_blank => true
# Brazilian UF validation # Both upcase and downcase are allowed by default validates_as_uf_br :uf # Age validation # :min_age and :max_age are both optional and the default values are 18.year and 100.year, respectively validates_as_age :birthday, :min_age => 10.year, :max_age => 50.year
end
I18n
The default error message is “is invalid”. You can customize this message via Rails I18n defining the key :invalid.
Example
Your customized en.yaml file would look like this:
en: activerecord: errors: messages: invalid: “MY CUSTOM MESSAGE”
Tests:
Some tests have been added to each validator.
Maintainers
-
Rafael Barbolo Lopes (github.com/barbolo)
-
Rafael Ivan Garcia (github.com/rafaelivan)
License:
MIT License. Copyright © 2011 by Infosimples. www.infosimples.com.br