validation_kit 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/validation_kit/validates_as_email/README +32 -0
- data/lib/validation_kit/validates_as_email/validates_as_email.rb +20 -0
- data/lib/validation_kit/validates_as_phone/README +69 -0
- data/lib/validation_kit/validates_as_phone/validates_as_phone.rb +122 -0
- data/lib/validation_kit/validates_as_postal_code/README +44 -0
- data/lib/validation_kit/validates_as_postal_code/validates_as_postal_code.rb +89 -0
- data/lib/validation_kit/validates_mixed_case_of/README +59 -0
- data/lib/validation_kit/validates_mixed_case_of/config/locales/en.yml +6 -0
- data/lib/validation_kit/validates_mixed_case_of/config/locales/fr.yml +6 -0
- data/lib/validation_kit/validates_mixed_case_of/validates_mixed_case_of.rb +40 -0
- data/lib/validation_kit/version.rb +3 -0
- data/lib/validation_kit.rb +8 -0
- data/validation_kit.gemspec +21 -0
- metadata +63 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
validates_as_email
|
2
|
+
==================
|
3
|
+
|
4
|
+
Validation module for email addresses.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
============
|
8
|
+
|
9
|
+
You can install the plugin the traditional way. Go to your application root
|
10
|
+
and do:
|
11
|
+
|
12
|
+
script/plugin install git://github.com/paulschreiber/validates_as_email.git
|
13
|
+
|
14
|
+
Example
|
15
|
+
=======
|
16
|
+
|
17
|
+
class Person < ActiveRecord::Base
|
18
|
+
validates_as_email :email
|
19
|
+
end
|
20
|
+
|
21
|
+
p = Person.new(:email => "Bob")
|
22
|
+
p.valid? # false
|
23
|
+
|
24
|
+
p = Person.new(:email => "joe@foobar.com")
|
25
|
+
p.valid? # true
|
26
|
+
|
27
|
+
Just like with built-in ActiveRecord validations, you can optionally supply a :message parameter with a custom error. Or, you can use Rails' built-in localization system and
|
28
|
+
|
29
|
+
|
30
|
+
= License
|
31
|
+
|
32
|
+
Copyright (c) 2010 Paul Schreiber, released under the MIT license
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ValidationKit
|
2
|
+
class EmailValidator < ActiveModel::EachValidator
|
3
|
+
# This hangs ruby if you have more than one validates_as_email rule in the same object
|
4
|
+
# Commenting it out for now
|
5
|
+
# Using the one found at <http://fightingforalostcause.net/misc/2006/compare-email-regex.php>
|
6
|
+
# EMAIL_ADDRESS_RE = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i
|
7
|
+
|
8
|
+
EMAIL_ADDRESS_RE = /^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/i
|
9
|
+
|
10
|
+
def validate_each(record, attribute, value)
|
11
|
+
unless EMAIL_ADDRESS_RE.match(value)
|
12
|
+
message = I18n.t("activerecord.errors.models.#{name.underscore}.attributes.#{attr_name}.invalid",
|
13
|
+
:default => [:"activerecord.errors.models.#{name.underscore}.invalid",
|
14
|
+
options[:message],
|
15
|
+
:'activerecord.errors.messages.invalid'])
|
16
|
+
record.errors[attribute] << message
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
validates_as_phone
|
2
|
+
==================
|
3
|
+
|
4
|
+
|
5
|
+
Strict validation module for phone numbers that supports classifications and
|
6
|
+
areas.
|
7
|
+
|
8
|
+
= General usage
|
9
|
+
|
10
|
+
== Installation
|
11
|
+
|
12
|
+
You can install the plugin the traditional way. Go to your application root
|
13
|
+
and do:
|
14
|
+
|
15
|
+
script/plugin install git://github.com/paulschreiber/validates_as_phone.git
|
16
|
+
|
17
|
+
== Validate your model attributes
|
18
|
+
|
19
|
+
Example:
|
20
|
+
|
21
|
+
There are three ways to specify the country:
|
22
|
+
(1) Pass the country name in as a string parameter (:country => "CA")
|
23
|
+
class Person < ActiveRecord::Base
|
24
|
+
validates_as_phone :phone, :country => "CA", :allow_blank => true, :set => true
|
25
|
+
end
|
26
|
+
|
27
|
+
(2) Create an attribute (country field) named foo in your object, and pass in a reference to foo:
|
28
|
+
|
29
|
+
class Person < ActiveRecord::Base
|
30
|
+
validates_as_phone :phone, :country => :foo
|
31
|
+
end
|
32
|
+
|
33
|
+
(3) Create an attribute named country it in your object, i.e. Person#country
|
34
|
+
i.e. country is the default name for your country field, and the validator tries that automatically
|
35
|
+
|
36
|
+
class Person < ActiveRecord::Base
|
37
|
+
validates_as_phone :phone
|
38
|
+
end
|
39
|
+
|
40
|
+
The :set parameter tells the validator to reformat the phone number (change punctuation and spacing) in to a standard format.
|
41
|
+
|
42
|
+
|
43
|
+
Supported countries are Canada (CA), the United States (US) and Australia (AU).
|
44
|
+
|
45
|
+
|
46
|
+
= License
|
47
|
+
|
48
|
+
Written by Kristina Lim (http://i-think.com.ph/kristina/)
|
49
|
+
|
50
|
+
Copyright (c) 2008 Syndeo Media
|
51
|
+
http://syndeomedia.com
|
52
|
+
|
53
|
+
US and Canada support added by Paul Schreiber (http://paulschreiber.com/)
|
54
|
+
Last modified 07 September 2010
|
55
|
+
Extension support added 17 November 2010
|
56
|
+
|
57
|
+
= Contributing
|
58
|
+
|
59
|
+
If you wish to contribute to the project, you may contact the author through:
|
60
|
+
|
61
|
+
'kristinasyndeomediacom'.insert(8, '@').insert(20, '.')
|
62
|
+
|
63
|
+
= Acknowledgements
|
64
|
+
|
65
|
+
This plugin is named after the validates_as_phone plugin of Jerrod Blavos
|
66
|
+
(http://code.google.com/p/validates-as-phone/). Admittedly, this plugin was
|
67
|
+
built over the latter, but as the purposes of this plugin is more complex than
|
68
|
+
that of the minimal one, practically all of the code has been written over by
|
69
|
+
now.
|
@@ -0,0 +1,122 @@
|
|
1
|
+
module ValidationKit
|
2
|
+
class PhoneValidator < ActiveModel::EachValidator
|
3
|
+
def regex_for_country(country_code)
|
4
|
+
if country_code.blank?
|
5
|
+
nil
|
6
|
+
elsif ["AU"].include?(country_code)
|
7
|
+
/(^(1300|1800|1900|1902)\d{6}$)|(^([0]?[1|2|3|7|8])?[1-9][0-9]{7}$)|(^13\d{4}$)|(^[0]?4\d{8}$)/
|
8
|
+
elsif ["US", "CA"].include?(country_code)
|
9
|
+
/^1?[2-9]\d{2}[2-9]\d{2}\d{4}/
|
10
|
+
else
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_each(record, attribute, value)
|
16
|
+
if options[:country].is_a?(String)
|
17
|
+
country = options[:country]
|
18
|
+
elsif options[:country].is_a?(Symbol) && record.respond_to?(options[:country])
|
19
|
+
country = record.send(options[:country])
|
20
|
+
elsif record.respond_to?(:country)
|
21
|
+
country = record.send(:country)
|
22
|
+
else
|
23
|
+
country = false
|
24
|
+
end
|
25
|
+
|
26
|
+
next unless country
|
27
|
+
current_regex = regex_for_country(country)
|
28
|
+
next unless current_regex
|
29
|
+
|
30
|
+
new_value = value.to_s.gsub(/[^0-9]/, '')
|
31
|
+
new_value ||= ''
|
32
|
+
|
33
|
+
message = I18n.t("activerecord.errors.models.#{name.underscore}.attributes.#{attr_name}.invalid",
|
34
|
+
:default => [:"activerecord.errors.models.#{name.underscore}.invalid",
|
35
|
+
options[:message], :'activerecord.errors.messages.invalid'])
|
36
|
+
|
37
|
+
unless (options[:allow_blank] && new_value.blank?) || new_value =~ current_regex
|
38
|
+
record.errors[attribute] << message
|
39
|
+
else
|
40
|
+
if options[:set]
|
41
|
+
formatted_phone = format_as_phone(value, country, options[:area_key])
|
42
|
+
if formatted_phone.nil?
|
43
|
+
record.errors[attribute] << message
|
44
|
+
else
|
45
|
+
record.send(attr_name.to_s + '=', formatted_phone)
|
46
|
+
end
|
47
|
+
end # options
|
48
|
+
end # unless
|
49
|
+
end
|
50
|
+
|
51
|
+
def format_as_phone(arg, country_code = nil, area_key = nil)
|
52
|
+
return nil if (arg.blank? or country_code.blank? or !regex_for_country(country_code))
|
53
|
+
|
54
|
+
number = arg.gsub(/[^0-9]/, '')
|
55
|
+
|
56
|
+
if country_code == "AU"
|
57
|
+
if number =~ /^(1300|1800|1900|1902)\d{6}$/
|
58
|
+
number.insert(4, ' ').insert(8, ' ')
|
59
|
+
elsif number =~ /^([0]?[1|2|3|7|8])?[1-9][0-9]{7}$/
|
60
|
+
if number =~ /^[1-9][0-9]{7}$/
|
61
|
+
number = number.insert(0, area_code_for_key(area_key))
|
62
|
+
end
|
63
|
+
number = number.insert(0, '0') if number =~ /^[1|2|3|7|8][1-9][0-9]{7}$/
|
64
|
+
|
65
|
+
number.insert(0, '(').insert(3, ') ').insert(9, ' ')
|
66
|
+
elsif number =~ /^13\d{4}$/
|
67
|
+
number.insert(2, ' ').insert(5, ' ')
|
68
|
+
elsif number =~ /^[0]?4\d{8}$/
|
69
|
+
number = number.insert(0, '0') if number =~ /^4\d{8}$/
|
70
|
+
|
71
|
+
number.insert(4, ' ').insert(8, ' ')
|
72
|
+
else
|
73
|
+
number
|
74
|
+
end
|
75
|
+
elsif ["CA", "US"].include?(country_code)
|
76
|
+
digit_count = number.length
|
77
|
+
# if it's too short
|
78
|
+
if digit_count < 10
|
79
|
+
return number
|
80
|
+
end
|
81
|
+
|
82
|
+
# strip off any leading ones
|
83
|
+
if number[0..0] == "1"
|
84
|
+
number = number[1..10]
|
85
|
+
end
|
86
|
+
|
87
|
+
area_code = number[0..2]
|
88
|
+
exchange = number[3..5]
|
89
|
+
sln = number[6..9]
|
90
|
+
|
91
|
+
if digit_count == 10
|
92
|
+
extension = nil
|
93
|
+
else
|
94
|
+
# save everything after the SLN as extension
|
95
|
+
sln_index = arg.index(sln)
|
96
|
+
# if something went wrong, return nil so we can error out
|
97
|
+
# i.e. 519 444 000 ext 123 would cause sln to be 0001, which is not found
|
98
|
+
# in the original string
|
99
|
+
return nil if sln_index.nil?
|
100
|
+
extension = " %s" % arg[(sln_index+4)..-1].strip
|
101
|
+
end
|
102
|
+
|
103
|
+
"(%s) %s-%s%s" % [area_code, exchange, sln, extension]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def area_code_for_key(key)
|
108
|
+
case key
|
109
|
+
when 'NSW' then '02'
|
110
|
+
when 'ACT' then '02'
|
111
|
+
when 'VIC' then '03'
|
112
|
+
when 'TAS' then '03'
|
113
|
+
when 'QLD' then '07'
|
114
|
+
when 'SA' then '08'
|
115
|
+
when 'NT' then '08'
|
116
|
+
when 'WA' then '08'
|
117
|
+
else
|
118
|
+
'02'
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
validates_as_postal_code
|
2
|
+
=====================
|
3
|
+
|
4
|
+
Strict validation module for postal and zip codes.
|
5
|
+
|
6
|
+
= General usage
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
|
10
|
+
You can install the plugin the traditional way. Go to your application root
|
11
|
+
and do:
|
12
|
+
|
13
|
+
script/plugin install git://github.com/paulschreiber/validates_as_postal_code.git
|
14
|
+
|
15
|
+
== Validate your model attributes
|
16
|
+
|
17
|
+
Example:
|
18
|
+
|
19
|
+
There are three ways to specify the country:
|
20
|
+
(1) Pass the country name in as a string parameter (:country => "CA")
|
21
|
+
class Person < ActiveRecord::Base
|
22
|
+
validates_as_postal_code :postal_code, :country => "CA", :allow_blank => true
|
23
|
+
end
|
24
|
+
|
25
|
+
(2) Create an attribute (country field) named foo in your object, and pass in a reference to foo:
|
26
|
+
class Person < ActiveRecord::Base
|
27
|
+
validates_as_postal_code :postal_code, :country => :foo
|
28
|
+
end
|
29
|
+
|
30
|
+
(3) Create an attribute named country it in your object, i.e. Person#country
|
31
|
+
i.e. country is the default name for your country field, and the validator tries that automatically
|
32
|
+
|
33
|
+
class Person < ActiveRecord::Base
|
34
|
+
validates_as_postal_code :postal_code
|
35
|
+
end
|
36
|
+
|
37
|
+
The :set parameter tells the validator to reformat the postal_code number (change punctuation and spacing) in to a standard format.
|
38
|
+
|
39
|
+
Supported countries are Canada (CA), the United States (US), Australia (AU), New Zealand (NZ). :set only works for Canada and the US; it's ignored for other countries.
|
40
|
+
|
41
|
+
|
42
|
+
= License
|
43
|
+
|
44
|
+
Copyright (c) 2010 Paul Schreiber, released under the MIT license
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module ValidationKit
|
2
|
+
class PostalCodeValidator < ActiveModel::EachValidator
|
3
|
+
|
4
|
+
def postal_code_regex_for_country(country_code)
|
5
|
+
if country_code.blank?
|
6
|
+
nil
|
7
|
+
elsif ["AU", "NZ"].include?(country_code)
|
8
|
+
/\d{4}/
|
9
|
+
elsif ["US"].include?(country_code)
|
10
|
+
/\d{5}(-\d{4})?/
|
11
|
+
elsif ["CA"].include?(country_code)
|
12
|
+
/[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTWVXYZ]\d[ABCEGHJKLMNPRSTWVXYZ]\d/
|
13
|
+
else
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def disallowed_characters_for_country(country_code)
|
19
|
+
if country_code.blank?
|
20
|
+
nil
|
21
|
+
elsif ["US", "AU", "NZ"].include?(country_code)
|
22
|
+
/[^0-9]/
|
23
|
+
elsif ["CA", "UK"].include?(country_code)
|
24
|
+
/[^0-9A-Z]/
|
25
|
+
else
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_each(record, attribute, value)
|
31
|
+
if options[:country].is_a?(String)
|
32
|
+
country = options[:country]
|
33
|
+
elsif options[:country].is_a?(Symbol) and record.respond_to?(options[:country])
|
34
|
+
country = record.send(options[:country])
|
35
|
+
elsif record.respond_to?(:country)
|
36
|
+
country = record.send(:country)
|
37
|
+
else
|
38
|
+
country = false
|
39
|
+
end
|
40
|
+
|
41
|
+
next unless country
|
42
|
+
current_regex = postal_code_regex_for_country(country)
|
43
|
+
next unless current_regex
|
44
|
+
disallowed_characters = disallowed_characters_for_country(country)
|
45
|
+
|
46
|
+
new_value = value.nil? ? "" : value.upcase.gsub(disallowed_characters, '')
|
47
|
+
|
48
|
+
unless (options[:allow_blank] && new_value.blank?) || new_value =~ current_regex
|
49
|
+
message = I18n.t("activerecord.errors.models.#{name.underscore}.attributes.#{attribute}.invalid",
|
50
|
+
:default => [:"activerecord.errors.models.#{name.underscore}.invalid",
|
51
|
+
options[:message],
|
52
|
+
:'activerecord.errors.messages.invalid'])
|
53
|
+
record.errors[attribute] << message
|
54
|
+
else
|
55
|
+
record.send(attr_name.to_s + '=',
|
56
|
+
format_as_postal_code(new_value, country, disallowed_characters)
|
57
|
+
) if options[:set]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def format_as_postal_code(arg, country_code, disallowed_characters)
|
62
|
+
return nil if (arg.blank? or country_code.blank? or !postal_code_regex_for_country(country_code))
|
63
|
+
|
64
|
+
postal_code = arg.gsub(disallowed_characters, '')
|
65
|
+
|
66
|
+
if ["US"].include?(country_code)
|
67
|
+
digit_count = postal_code.length
|
68
|
+
if digit_count == 5
|
69
|
+
return postal_code
|
70
|
+
elsif digit_count == 9
|
71
|
+
return "%s-%s" % [postal_code[0..4], postal_code[5..8]]
|
72
|
+
else
|
73
|
+
return nil
|
74
|
+
end
|
75
|
+
|
76
|
+
elsif ["AU", "NZ"].include?(country_code)
|
77
|
+
postal_code
|
78
|
+
|
79
|
+
elsif ["CA"].include?(country_code)
|
80
|
+
fsa = postal_code[0..2]
|
81
|
+
lda = postal_code[3..5]
|
82
|
+
|
83
|
+
postal_code = "%s %s" % [fsa, lda]
|
84
|
+
postal_code.upcase
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
Validates Mixed Case Of
|
2
|
+
=======================
|
3
|
+
|
4
|
+
When validating a string item, ensure it is not all in capital or lowercase letters.
|
5
|
+
|
6
|
+
Example
|
7
|
+
=======
|
8
|
+
|
9
|
+
class Person < ActiveRecord::Base
|
10
|
+
validates_mixed_case_of :firstname
|
11
|
+
end
|
12
|
+
|
13
|
+
p = Person.new(:firstname => "BOB")
|
14
|
+
p.valid? # false
|
15
|
+
p = Person.new(:firstname => "Bob")
|
16
|
+
p.valid? # true
|
17
|
+
|
18
|
+
Attribute Names
|
19
|
+
===============
|
20
|
+
Attribute names are looked up using Rails' I18n system. Create a hierarchy like so:
|
21
|
+
|
22
|
+
en:
|
23
|
+
activerecord:
|
24
|
+
models:
|
25
|
+
attributes:
|
26
|
+
person:
|
27
|
+
firstname: "first name"
|
28
|
+
|
29
|
+
Alternately, pass in the :attribute_name option:
|
30
|
+
|
31
|
+
class Person < ActiveRecord::Base
|
32
|
+
validates_mixed_case_of :firstname, :attribute_name => "first name"
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
Error Messages
|
37
|
+
==============
|
38
|
+
Error messages follow the standard ActiveRecord lookup method.
|
39
|
+
|
40
|
+
You'll likely error messages can be customized like so:
|
41
|
+
|
42
|
+
en:
|
43
|
+
activerecord:
|
44
|
+
errors:
|
45
|
+
models:
|
46
|
+
attributes:
|
47
|
+
person:
|
48
|
+
firstname:
|
49
|
+
all_caps: "first name should not be in all caps"
|
50
|
+
|
51
|
+
Alternatively, pass in tje :all_caps option:
|
52
|
+
|
53
|
+
class Person < ActiveRecord::Base
|
54
|
+
validates_mixed_case_of :firstname, :all_caps => "don't shout at me, bro"
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
Copyright 2011 Paul Schreiber, released under the MIT license
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module ValidationKit
|
2
|
+
class MixedCaseValidator < ActiveModel::EachValidator
|
3
|
+
ALL_CAPS = 1
|
4
|
+
ALL_LOWERCASE = -1
|
5
|
+
|
6
|
+
def validate_each(record, attribute, value)
|
7
|
+
next if value.nil?
|
8
|
+
next if value.gsub(/\W/, "").size < 3 # skip very short words
|
9
|
+
error = nil
|
10
|
+
|
11
|
+
if (value.upcase == value)
|
12
|
+
error = ALL_CAPS
|
13
|
+
elsif (value.downcase == value)
|
14
|
+
error = ALL_LOWERCASE
|
15
|
+
end
|
16
|
+
|
17
|
+
next if error.nil?
|
18
|
+
|
19
|
+
item_name = I18n.t("activerecord.models.attributes.#{name.underscore}.#{attribute}",
|
20
|
+
:default => nil) or options[:attribute_name] or attribute
|
21
|
+
|
22
|
+
if error == ALL_CAPS
|
23
|
+
message = I18n.t("activerecord.errors.models.attributes.#{name.underscore}.#{attr_name}.all_caps",
|
24
|
+
:item => item_name,
|
25
|
+
:default => [:"activerecord.errors.models.#{name.underscore}.all_caps",
|
26
|
+
options[:all_caps],
|
27
|
+
:'activerecord.errors.messages.all_caps'])
|
28
|
+
elsif error == ALL_LOWERCASE
|
29
|
+
message = I18n.t("activerecord.errors.models.attributes.#{name.underscore}.#{attr_name}.all_lowercase",
|
30
|
+
:item => item_name,
|
31
|
+
:default => [:"activerecord.errors.models.#{name.underscore}.all_lowercase",
|
32
|
+
options[:all_lowercase],
|
33
|
+
:'activerecord.errors.messages.all_lowercase'])
|
34
|
+
end
|
35
|
+
|
36
|
+
record.errors[attribute] << message
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "validation_kit/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "validation_kit"
|
7
|
+
s.version = ValidationKit::VERSION
|
8
|
+
s.authors = ["Wes Morgan", "Paul Schreiber"]
|
9
|
+
s.email = ["wes@turbovote.org", "paul@turbovote.org"]
|
10
|
+
s.homepage = "https://github.com/turbovote/validation_kit"
|
11
|
+
s.summary = %q{Handy validations for Rails forms}
|
12
|
+
s.description = %q{A collection of various validators for Rails forms}
|
13
|
+
|
14
|
+
s.rubyforge_project = "validation_kit"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validation_kit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Wes Morgan
|
9
|
+
- Paul Schreiber
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-03-08 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: A collection of various validators for Rails forms
|
16
|
+
email:
|
17
|
+
- wes@turbovote.org
|
18
|
+
- paul@turbovote.org
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- Rakefile
|
26
|
+
- lib/validation_kit.rb
|
27
|
+
- lib/validation_kit/validates_as_email/README
|
28
|
+
- lib/validation_kit/validates_as_email/validates_as_email.rb
|
29
|
+
- lib/validation_kit/validates_as_phone/README
|
30
|
+
- lib/validation_kit/validates_as_phone/validates_as_phone.rb
|
31
|
+
- lib/validation_kit/validates_as_postal_code/README
|
32
|
+
- lib/validation_kit/validates_as_postal_code/validates_as_postal_code.rb
|
33
|
+
- lib/validation_kit/validates_mixed_case_of/README
|
34
|
+
- lib/validation_kit/validates_mixed_case_of/config/locales/en.yml
|
35
|
+
- lib/validation_kit/validates_mixed_case_of/config/locales/fr.yml
|
36
|
+
- lib/validation_kit/validates_mixed_case_of/validates_mixed_case_of.rb
|
37
|
+
- lib/validation_kit/version.rb
|
38
|
+
- validation_kit.gemspec
|
39
|
+
homepage: https://github.com/turbovote/validation_kit
|
40
|
+
licenses: []
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project: validation_kit
|
59
|
+
rubygems_version: 1.8.17
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Handy validations for Rails forms
|
63
|
+
test_files: []
|