Skip to content

Latest commit

 

History

History
124 lines (82 loc) · 3.07 KB

README.rdoc

File metadata and controls

124 lines (82 loc) · 3.07 KB

Rails 3 validators

Currently only using generators to copy validators into your app. In the future it should be supplied as a Rails 3 Engine ;)

TODO

Encapsulate all the name validators into one validator that takes arguments

validates :name, :name => {:spaces => [:one, :multi], :allow => ‘chars’, :deny => ‘chars’, :alpha => [:strict, :euro], :cap => [:word, :none], :numbers => [:allow, :not_first], :connectors => [true, ‘_-’]}

By default no spaces and only alpha a-zA-Z. Numbers can be allowed or allowed only after first letter.

Provides these validators:

Attribute validators

  • company_name

  • person_name

  • full_name

  • name

  • alpha_name

  • ruby_name

  • email_format (using regexp)

  • email (using mail)

  • url_format

  • currency_validator

Person name:

class Person
  ...
  validates :name, :presence => true, :length => {:within => 2..12}, :person_name => true
end

Alpha name:

class Executer
  ...
  validates :method, :alpha_name => true
end

Company name:

class Company
  ...
  validates :name, :length => {:within => 2..40}, :company_name => true
end

Email format:

class ContactInfo
  ...
  validates :email, :presence => true, :email_format => true
end

Email:

class ContactInfo
  ...
  validates :email, :presence => true, :email => true
end

Url:

class Profile
  ...
  validates :webpage, :presence => true, :url_format => true
end

Configuration

To finetune the name validators:

ValidatorNameConfig.name_length 3..40
ValidatorNameConfig.name_expr /[a-z]+/

This will make the name validators test for a name length between 2 and 40 characters! Note: The company_name validator always allows for a single character name, unless overridden explicitly using the :length option.

Complex validators

  • full_name

Usage:

class Person
  ...
  validates_with FullNameValidator 
end

Generator

Use the provided Rails generator to copy the validators directly into your Rails app so they are available to your models

$ rails_validators [list of validators]

Example: Copy all validators (default)

$ rails_validators

Example: Copy specific validators

$ rails_validators email_format url_format

TODO

Add a state/province validator, perhaps using the ‘states_select’ project. Perhaps add a list_value_validator which loads the list to check from a yaml file

Contributing to rails-validators

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2010 Kristian Mandrup. See LICENSE.txt for further details.